Java 第一个数字越大,它就不起作用
当我输入的第一个数字大于第二个数字时,程序不会输出任何东西,它只是空白Java 第一个数字越大,它就不起作用,java,Java,当我输入的第一个数字大于第二个数字时,程序不会输出任何东西,它只是空白 import javax.swing.*; //for JFrame and JPanel import java.text.*; //for text public class numbe
import javax.swing.*; //for JFrame and JPanel
import java.text.*; //for text
public class number6{ //states class name
public static void main (String [] args){ //starts program
String firstStr, secondStr; //states string variables
int first, second; //states integer variables
DecimalFormat df = new DecimalFormat("0.000");
firstStr = JOptionPane.showInputDialog(null, "Enter first number:"); //asks for first number
first = Integer.parseInt(firstStr); //makes first number from string to integer
secondStr = JOptionPane.showInputDialog(null, "Enter second number:"); //asks for second number
second = Integer.parseInt(secondStr); //makes second nubmer from string to integer
for(int x = first; x <= second; x++){
if(first != 0){ //doesn't show 0
System.out.print(x + " "); //output statement
}
}
}
}
import javax.swing.*//对于JFrame和JPanel
导入java.text.*//用于文本
公共类号6{//表示类名
公共静态void main(字符串[]args){//启动程序
String firstStr,secondStr;//表示字符串变量
int first,second;//表示整数变量
DecimalFormat df=新的DecimalFormat(“0.000”);
firstStr=JOptionPane.showInputDialog(null,“输入第一个数字:”);//要求输入第一个数字
first=Integer.parseInt(firstStr);//从字符串到整数生成第一个数字
secondStr=JOptionPane.showInputDialog(null,“输入第二个数字:”);//要求输入第二个数字
second=Integer.parseInt(secondStr);//从字符串到整数生成第二个numer
对于(int x=first;x您正在设置x=first
,然后增加x
,只要x
小于或等于second
。如果first>second
,则永远不会满足此条件
您需要确保从最小值迭代到最大值:
for(int x = Math.min(first, second); x <= Math.max(first, second); x++)
for(int x=Math.min(first,second);x请修复缩进。这是不可读的,只会让你更难理解。“程序不工作”--你能解释一下这意味着什么吗?你收到错误消息了吗?你的键盘着火了吗?华夫饼干烧焦了吗?如果第一个数字更大,你希望它做什么?