Java 使用 do-while 循环
Java Using do-while loop
使用 do-while 循环,编写一个程序,要求输入五 (5) 个整数。确定最高值和最低值。
考虑示例输出。
输出:
Enter 1st number: 5
Enter 2nd number: 10
Enter 3rd number: 2
Enter 4th number: 50
Enter 5th number: 30
The highest value is: 50
The lowest value is: 2
老实说,我不知道如何回答这个问题,因为我的教授还没有讨论过使用 do while 循环来确定数字。到目前为止,这就是我所拥有的
编辑:
我最终得到了这段代码,但我不知道如何显示第 1、2、3、4、5。
enter code here
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inp=new Scanner(System.in);
int a = 1;
int i =1;
int highestNumber=0;
int lowestNumber=0;
while (i < 5) {
System.out.println("Enter " + a++ +" number ");
int numbers = inp.nextInt();
if(highestNumber==0) {
highestNumber = numbers;
}else if(numbers>highestNumber) {
highestNumber=numbers;
};
if(lowestNumber==0) {
lowestNumber = numbers;
}else if(numbers<lowestNumber) {
lowestNumber=numbers;
};
System.out.println("The Highest Number is: " + highestNumber);
System.out.println("The Lowest Number is: " + lowestNumber);
}
}
在问这样的问题之前请做一些研究。 While-Loops 在整个互联网上都有详细的记录。也就是说,这应该可以工作(虽然还没有测试过):
public static void main(String[] args) {
Scanner inp=new Scanner(System.in);
int[] numbers = new int[5];
int i = 0;
while (i < numbers.length) {
System.out.println("Enter number " + i);
numbers[i] = inp.nextInt();
i++;
}
}
一些作业改进以满足要求:
import java.util.*;
public class Main {
public static void main(String[] args) {
String[] nums = new String[] {"1st", "2nd", "3rd", "4th", "5th"};
try(Scanner inp = new Scanner(System.in)) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int i = 0;
do{
System.out.print("Enter " + nums[i] + " number: ");
int num = inp.nextInt();
if(num < min) min = num;
if(num > max) max = num;
} while(++i < nums.length);
System.out.println("\nThe highest value is: " + max);
System.out.println("The lowest value is: " + min);
} catch(InputMismatchException e) {
e.printStackTrace();
}
}
}
使用 do-while 循环,编写一个程序,要求输入五 (5) 个整数。确定最高值和最低值。 考虑示例输出。
输出:
Enter 1st number: 5
Enter 2nd number: 10
Enter 3rd number: 2
Enter 4th number: 50
Enter 5th number: 30
The highest value is: 50
The lowest value is: 2
老实说,我不知道如何回答这个问题,因为我的教授还没有讨论过使用 do while 循环来确定数字。到目前为止,这就是我所拥有的
编辑: 我最终得到了这段代码,但我不知道如何显示第 1、2、3、4、5。
enter code here
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner inp=new Scanner(System.in);
int a = 1;
int i =1;
int highestNumber=0;
int lowestNumber=0;
while (i < 5) {
System.out.println("Enter " + a++ +" number ");
int numbers = inp.nextInt();
if(highestNumber==0) {
highestNumber = numbers;
}else if(numbers>highestNumber) {
highestNumber=numbers;
};
if(lowestNumber==0) {
lowestNumber = numbers;
}else if(numbers<lowestNumber) {
lowestNumber=numbers;
};
System.out.println("The Highest Number is: " + highestNumber);
System.out.println("The Lowest Number is: " + lowestNumber);
}
}
在问这样的问题之前请做一些研究。 While-Loops 在整个互联网上都有详细的记录。也就是说,这应该可以工作(虽然还没有测试过):
public static void main(String[] args) {
Scanner inp=new Scanner(System.in);
int[] numbers = new int[5];
int i = 0;
while (i < numbers.length) {
System.out.println("Enter number " + i);
numbers[i] = inp.nextInt();
i++;
}
}
一些作业改进以满足要求:
import java.util.*;
public class Main {
public static void main(String[] args) {
String[] nums = new String[] {"1st", "2nd", "3rd", "4th", "5th"};
try(Scanner inp = new Scanner(System.in)) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int i = 0;
do{
System.out.print("Enter " + nums[i] + " number: ");
int num = inp.nextInt();
if(num < min) min = num;
if(num > max) max = num;
} while(++i < nums.length);
System.out.println("\nThe highest value is: " + max);
System.out.println("The lowest value is: " + min);
} catch(InputMismatchException e) {
e.printStackTrace();
}
}
}