Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
public class Main {
public static void main(String[] args) {
int z;
int [] a = new int[5];
a[0]=4;
a[1]=8;
a[2]=5;
a[3]=1;
a[4]=3;
for(;;){
z=0;
for(int i=1;i<a.length;i++){
if(a[i-1]>a[i]){
int tmp = a[i];
a[i-1]=a[i];
a[i]=tmp;
z++;
}
}
if(z==0){
break;
}
}
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}}
Hi. I have this problem. I want to sort array items, but result of this code is 1 1 1 1 3. I can't understand where is problem.
Thanks you very much!
You overwrite the value of a[i-1] thus you lose it
So you have just to change this int tmp = a[i]; with this int tmp = a[i-1];
The whole code is :
public class MailClient {
public static void main(String[] args) {
int z;
int [] a = new int[5];
a[0]=4;
a[1]=8;
a[2]=5;
a[3]=1;
a[4]=3;
for(;;){
z=0;
for(int i=1;i<a.length;i++){
if(a[i-1]>a[i]){
int tmp = a[i-1];
a[i-1]=a[i];
a[i]=tmp;
z++;
}
}
if(z==0){
break;
}
}
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
package p22_09_24;
public class Lotto2 {
public static void main(String[] args) {
for (int i=0; i <6; i++) {
i = (int)(Math.random()*45)+1;
System.out.println(i);
}
}
}
As you can see, the exit is under the 'for' which means it should loop six times, but the answer is only one num.
public class Random {
public static void main(String[] args) {
for (int i = 0; i < 6; i++)
{
int randomNumber = (int) (Math.random()*45)+1;
System.out.println("Random number is: " + randomNumber);
}
}
}
use different name to store the random number.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
These are two of the same code, the first one is in C++, and the other is in Java. The C++ code is compiling successfully, while the Java code gives an error:
last is not initialized
but at the end, the value of i will be the last. But it is throwing an error, please help me to figure it out.
C++ code:
#include <iostream>
using namespace std;
int main()
{
int arr[] = {1,2,3,4,5,6};
int last;
for(int i=0;i<6;i++)
{
cout<<arr[i];
last = i;
}
cout<<last;
return 0;
}
Output:
1234565
Java code:
public class Main
{
public static void main(String[] args) {
int arr[] = {1,2,3,4,5,6};
int last;
for(int i=0;i<6;i++)
{
System.out.print(arr[i]);
last = i;
}
System.out.print(last);
}
}
Output:
Main.java:19: error: variable last might not have been initialized
System.out.print(last);
^
1 error
It seems to me that you didn't initialise the variable first. Initializing them is setting them equal to a value:
int a; // This is a declaration
a = 0; // This is an initialization
int b = 1; // This is a declaration and initialization
Please note, Java primitives have default values but as one user commented below
Their default value is zero when declared as class members. Local variables don't have default values
Try the following:
public static void main(String args[]) {
int arr[] = {1,2,3,4,5,6};
int last = 0;
for(int i=0;i<6;i++)
{
System.out.print(arr[i]);
last = i;
}
System.out.print(last);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Kindly help me with the underlying program as I am stuck. I'm a newbie programmer.
import java.util.*;
public class Source
{
static int maxProduct(int arr[]) {
int n = arr.length ;
if (n < 2)
{
System.out.println("NA");
return Integer.MIN_VALUE;
}
int a = arr[0];
int b = arr[1];
for(int i = 0; i<n; i++) {
for (int j = i+1; j<n; j++) {
if (arr[i]*arr[j] > arr[0]*arr[1]) {
a = arr[i];
b = arr[j];
}
}
}
return maxProduct;
}
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int size = s.nextInt();
int[] arr = new int[size];
for(int i = 0; i < size; i++) {
arr[i] = s.nextInt();
}
int answer = maxProduct(arr);
System.out.print(answer);
}
}
You should change
if (arr[i]*arr[j] > arr[0]*arr[1])
to
if (arr[i]*arr[j] > a * b)
Since arr[0]*arr[1] is just the original max product, so you shouldn't be comparing against it.
Also note that your solution is not as efficient as it can be, since you are using a nested loop, which requires O(n^2) running time.
You can achieve linear (O(n)) running time if you use the fact that the max product is either the product of the two highest positive values or the product of the two lowest negative values. This means that if you find these 4 numbers, which can be done with a single loop, you'll find the max product.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
public static void main(String[] args) {
int [][] a={{1,2,3},{4,5,6}} ;
for(int x:a)
System.out.print(" "+x);
}
}
Could someone please tell me how would I print each value in the array?
You have to use for each loop twice. In first loop you iterate over all the array and in the inner loop you iterate over all the integers in the outer array.
int [][] a={{1,2,3},{4,5,6}} ;
for(int []x:a){
for(int y : x){
System.out.println(y);
}
}
As you're having a dual-dimension array, you need two loops:
public static void main(String[] args) {
int [][] a={{1,2,3},{4,5,6}} ;
for(int[] r:a) {
for (int x:r) {
System.out.print(" "+x);
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
import java.util.*;
import javax.swing.*;
public class Practice {
public static void main(String[] args){
int[] numbers = {1, 2, 3, 14, 15, 16, 17};
getSmall(numbers);
}
public static void getSmall(int[] ar){
int small=0;
for(int i=0; i<ar.length; i++){
if(ar[i]<small)
small = ar[i];
}
System.out.println(small);
}
}
The program is to find the smallest number in the array, there is no compiler error but it doesn't show the correct result.
Thank you in advance!
public static void getSmall(int[] ar){
int small=ar[0];
for(int i=1; i<ar.length; i++){
if(ar[i]<small)
small = ar[i];
}
System.out.println(small);
}
Setting small initially to 0 is a bad idea as you are hoping that your array contains an element less than that for the answer to be correct.
A common idiom is to initialise small to ar[0] (having, of course, first checked that ar contains at least 1 element). Then run your loop from 1.
for(int i = 1;
(I dislike initialising small to a very large number since that puts the answer to your function in an undefined state if ar does not have any elements.)
Change
int small = 0
to
int small = ar[0];
Don't init small=0,
change to int small = ar[0]
You can also use
int small=Integer.MAX_VALUE;
in place of
int small=0;
this code will print small value, give the max of integer
Change this
public static void getSmall(int[] ar) {
int small = 0;
for (int i = 0; i < ar.length; i++) {
if (ar[i] < small)
small = ar[i];
}
System.out.println(small);
}
to
public static void getSmall(int[] ar) {
int small = ar[0];
for (int i = 1; i < ar.length; i++) {
if (ar[i] < small)
small = ar[i];
}
System.out.println(small);
}
because variable small is not assigned to any value from your array