3 回答
TA贡献1877条经验 获得超6个赞
根据我的观点,我认为您需要像您的示例一样的答案。因此,我为您制作了一个程序。在这个程序中如果你只先输入大数,你可以用except if语句,这是我的解决方案。
import java.util.*;
import java.lang.*;
public class Stack2{
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
int num1=sc.nextInt();
int num2=sc.nextInt();
if(num1<num2){
System.out.println("Number 1 is less than number 2");
System.exit(1);
}
int x=num1-num2;
System.out.print(num1+" - "+num2+" --> is equal to "+ num1+" " );
while(num1!=x){
System.out.print("-1 ");
num1--;
}
System.out.println("= "+x);
}
}
TA贡献2021条经验 获得超8个赞
您的代码几乎不需要更正。您尚未声明 Scanner 对象正确和偶数。试试这个代码,
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
System.out.print(num1 + " - " + num2 + " --> Is equal to " + num1);
while(num2 > 0) {
System.out.print(" - 1");
num1 -= 1;
num2--;
}
System.out.println(" = " + num1);
sc.close();
}
TA贡献1810条经验 获得超4个赞
我不确定你是否想要这样的
int num1 = 8;
int num2 = 5;
int res = num1- num2;
boolean bandera = Boolean.TRUE;
String salida = "";
while(bandera)
{
if(num2 > 0)
{
salida = salida +"-1";
num2--;
}else
{
bandera = Boolean.FALSE;
}
}
System.out.println(num1 + salida + "=" + res);
添加回答
举报