如果扫描仪输入为负数,则不显示任何内容。 如果我输入-11,那么10,-10和-1应该是输出。为什么负循环输入在for循环中不起作用
import java.util.Scanner;
public class Factor
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String replay = "replay";
while(replay.equals("replay"))
{
System.out.println("The numbers that add to be ___(number1)___ that multiply to be ___(number2)___ are...");
System.out.println("Enter number1:");
int n1 = scan.nextInt();
System.out.println("Enter number2:");
int n2 = scan.nextInt();
System.out.println("Computing...");
for(double f2 = -1000; f2 <= n1; f2++)
{
for(double f1 = -1000; f1 <= n1; f1++)
{
if(f1*f2 == n2 && f1+f2 == n1)
{
System.out.println(f1 + " and " + f2);
}
}
}
scan.nextLine();
System.out.println("Enter replay if you would like to compute again");
replay = scan.nextLine();
}
}
}
即使我的循环变量是负值。
你应该让F1和F2的整数,而不是混合双打和整数在计算中。你可以用双打来解决问题,例如1.00000000000001!= 1。虽然 – slipperyseal
我不是说这是你的问题,但一直迭代到1000解决你的问题? – handris