Someone please help me on this. I think the error in my code is in the if statements, but I am not sure.
Thanks in advance.
import java.util.Scanner;
public class Largest
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int cnt = 0; // loop control variable
int num = 0;
int largest = 0; // stores the largest number
System.out.println("Enter 10 numbers: ");
do
{
num = in.nextInt();
if(largest > num)
largest = largest;
if else(largest < num)
largest = num;
cnt++;
}
while(cnt <= 10);
}
}
A for loop would be better, just saying. Also, why are you setting
largest = largest;
That makes no sense in any way. Largest is already that amount.
Here is what I would do
import java.util.Scanner;
public class Largest{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num = 0;
int largest = 0;
System.out.println("Enter 10 numbers: ");
for(int x = 0; x < 10; x++) {
num = in.nextInt();
if(num > largest)
largest = num;
}
}
}
It may have errors since I did it in the text box directly.
I tested it and I got an exception, probably something to do with the scanner, looked up oracle stuff, looks like I did stuff right.
Thanks in advance.
All aboard the SwaggaTrain...
That makes no sense in any way. Largest is already that amount.
Here is what I would do
It may have errors since I did it in the text box directly.
I tested it and I got an exception, probably something to do with the scanner, looked up oracle stuff, looks like I did stuff right.
Thinking about coming a mod to simply not moderate.