My assignment is to take an array of ten numbers of the user input, and then find the lowest and highest numbers.
我的任务是取一个十个数字的用户输入数组,然后找到最低和最高的数字。
I have done it according to class notes, but it is not working on netbeans.
我是根据课堂笔记完成的,但它不适用于netbeans。
import static java.lang.System.in;
import java.util.Scanner;
public class FindLargestSmallestNumber {
public static void main(String[] args) {
//array of 10 numbers
int numbers[] = new int[]{0,0,0,0,0,0,0,0,0,0};
int i=0;
for(int x=1; x<numbers.length; i++)
{
Scanner user_input = new Scanner( System.in );
System.out.println ("What is the "+ x +" number of the array");
numbers [x] = in.nextInt( );
}
//assign first element of an array to largest and smallest
int smallest = numbers[0];
int largest = numbers[0];
for(i=1; i< numbers.length; i++)
{
if(numbers[i] > largest)
largest = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largest);
System.out.println("Smallest Number is : " + smallest);
}
}
3 个解决方案
#1
2
Array index starts from 0
数组索引从0开始
In for loop start from index = 0 to index < 10
在for循环中从index = 0到index <10
import java.util.*;
class MAXIMUMorMINIMUM{
public static void main(String args[] ){
Scanner in = new Scanner(System.in);
//array of size 10
int N = 10;
int array[] = new int[N];
for(int i = 0; i < N ;i ++){
array[i] = in.nextInt();
}
int maximum = array[0];
int minimum = array[0];
for(int i = 0; i < N; i++){
//maximum
if(array[i] > maximum){
maximum = array[i];
}
//minimum
if (array[i] < minimum) {
minimum = array[i];
}
}
System.out.println("MAXIMUM : "+maximum);
System.out.println("MINIMUM : "+minimum);
}
}
#2
1
Your problem is you are incrementing i
in the for loop and not x
in the first for loop. That makes it run forever with x==1
. Each time the loop code runs you are changing i
which has no effect on the loop condition x<numbers.length
.
你的问题是你在for循环中递增i而不是在第一个for循环中递增x。这使得它在x == 1时永远运行。每次循环代码运行时,您正在更改i,这对循环条件x
You'll also want to start x
at 0
instead of 1
since you index arrays starting at 0
.
因为从0开始索引数组,所以您还希望从0开始,而不是1。
also, inside that for loop in
is actually System.in
since you statically imported it. you should write user_input.nextInt()
instead, since that the Scanner
you made. This kind of thing should be obvious to you, especially if you are using an IDE that highlights the stuff for you like netbeans.
另外,在for循环中实际上是System.in,因为你静态导入它。你应该编写user_input.nextInt(),因为你创建了扫描仪。这种事情对你来说应该是显而易见的,特别是如果你使用的IDE会像netbeans一样突出你的东西。
#3
0
The Code should look like
守则应该是这样的
public static void main(String[] args) {
//array of 10 numbers
int [] numbers = {0,0,0,0,0,0,0,0,0,0};
for(int x=0; x<numbers.length; x++)
{
Scanner user_input = new Scanner( System.in );
System.out.println ("What is the "+ x +" number of the array");
numbers [x] = user_input.nextInt();
}
//assign first element of an array to largest and smallest
int smallest = numbers[0];
int largest = numbers[0];
for(i=1; i< numbers.length; i++)
{
if(numbers[i] > largest)
largest = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largest);
System.out.println("Smallest Number is : " + smallest);
}
As you created the Scanner Object and need to use it to get Input object from the user Also to instantiate an Array you either call it like above or like the following:
当您创建扫描程序对象并需要使用它来从用户获取Input对象时也要实例化一个数组,您可以像上面一样调用它,或者像下面这样:
int[] numbers = new int [10];
Also Arrays index start at 0 not at 1
Arrays索引也从0开始,而不是1
#1
2
Array index starts from 0
数组索引从0开始
In for loop start from index = 0 to index < 10
在for循环中从index = 0到index <10
import java.util.*;
class MAXIMUMorMINIMUM{
public static void main(String args[] ){
Scanner in = new Scanner(System.in);
//array of size 10
int N = 10;
int array[] = new int[N];
for(int i = 0; i < N ;i ++){
array[i] = in.nextInt();
}
int maximum = array[0];
int minimum = array[0];
for(int i = 0; i < N; i++){
//maximum
if(array[i] > maximum){
maximum = array[i];
}
//minimum
if (array[i] < minimum) {
minimum = array[i];
}
}
System.out.println("MAXIMUM : "+maximum);
System.out.println("MINIMUM : "+minimum);
}
}
#2
1
Your problem is you are incrementing i
in the for loop and not x
in the first for loop. That makes it run forever with x==1
. Each time the loop code runs you are changing i
which has no effect on the loop condition x<numbers.length
.
你的问题是你在for循环中递增i而不是在第一个for循环中递增x。这使得它在x == 1时永远运行。每次循环代码运行时,您正在更改i,这对循环条件x
You'll also want to start x
at 0
instead of 1
since you index arrays starting at 0
.
因为从0开始索引数组,所以您还希望从0开始,而不是1。
also, inside that for loop in
is actually System.in
since you statically imported it. you should write user_input.nextInt()
instead, since that the Scanner
you made. This kind of thing should be obvious to you, especially if you are using an IDE that highlights the stuff for you like netbeans.
另外,在for循环中实际上是System.in,因为你静态导入它。你应该编写user_input.nextInt(),因为你创建了扫描仪。这种事情对你来说应该是显而易见的,特别是如果你使用的IDE会像netbeans一样突出你的东西。
#3
0
The Code should look like
守则应该是这样的
public static void main(String[] args) {
//array of 10 numbers
int [] numbers = {0,0,0,0,0,0,0,0,0,0};
for(int x=0; x<numbers.length; x++)
{
Scanner user_input = new Scanner( System.in );
System.out.println ("What is the "+ x +" number of the array");
numbers [x] = user_input.nextInt();
}
//assign first element of an array to largest and smallest
int smallest = numbers[0];
int largest = numbers[0];
for(i=1; i< numbers.length; i++)
{
if(numbers[i] > largest)
largest = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largest);
System.out.println("Smallest Number is : " + smallest);
}
As you created the Scanner Object and need to use it to get Input object from the user Also to instantiate an Array you either call it like above or like the following:
当您创建扫描程序对象并需要使用它来从用户获取Input对象时也要实例化一个数组,您可以像上面一样调用它,或者像下面这样:
int[] numbers = new int [10];
Also Arrays index start at 0 not at 1
Arrays索引也从0开始,而不是1