[英]Java program high-low game
I wrote this in jQuery however the loop isn't right and I can't see my error? 我是用jQuery写的,但是循环不正确,看不到错误? It doesn't end when a user inputs -99?
用户输入-99不会结束? Nor calculate the highest and lowest figure?
也不计算最高和最低数字?
import java.util.Scanner;
public class LargestandSmallest
{
public static void main (String [] args)
{
//Create a Scanner object for the keyboard input
Scanner keyboard = new Scanner(System.in);
//Declare local variable
double number;
//Explain what the program does
System.out.println ("This program will ask you to enter in a series of");
System.out.println ("numbers, and then will display the lowest and");
System.out.println ("highest numbers, out of what you enter, until you input");
System.out.println ("the value indicated to end the program.");
//Have the user input a series of numbers and continue processing
//until the user enters -99
System.out.println ("Please enter a number: (or -99 to end the program).");
number = keyboard.nextDouble();
//Display the table headings
System.out.println ("Lowest\tHighest");
System.out.println ("------------------");
//Call the method to caluclate the highest and lowest numbers
calculateLowHigh(number);
}
//Module called calculateTemp
public static void calculateLowHigh(double number)
{
//Declare local calculation variables
double highestNumber = 0;
double lowestNumber = 0;
//Set the parameters for running the loop
while (number != -99)
{
for(number = 0; number < 5; number++)
{
//Calculate the lowest and highest numbers entered
if (number > highestNumber) {
highestNumber = number;
}
if (number < lowestNumber) {
lowestNumber = number;
}
}
}
//Display the results
System.out.println (lowestNumber + "\t\t" + highestNumber);
}
}
Your problem is that in calculateLowHigh
, you're using the number
variable for two different things. 您的问题是在
calculateLowHigh
,您将number
变量用于两个不同的事物。 It's the parameter in which the number that the user typed is passed into this method; 这是将用户键入的数字传递到此方法的参数; but you've also used it as the loop index inside the
for
loop. 但您也已将其用作
for
循环中的循环索引。 Maybe you should use a different variable for one or the other purpose. 也许您应该将一个变量用于另一个目的。 Better still, dispense with the
for
loop altogether - it doesn't seem to do anything. 更好的是,完全省去了
for
循环-它似乎没有任何作用。
Also, the while
loop should be done in main
, not in calculateHighLow
, otherwise the user will only ever have the opportunity to input a number once. 另外,
while
循环应该在main
而不是在calculateHighLow
,否则用户将只能输入一次数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.