本文介绍了Java:如何正确使用带扫描仪的循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好



我想写一个程序,将一个范围内的两个数字相乘



这是我的代码:







Scanner mys = new Scanner(System.in);



System.out.println(1);

int num1 = mys.nextInt();



System.out.println(12);

int num12 = mys.nextInt();



System.out.println(2);

int num2 = mys.nextInt();



System.out。 println(22);

int num22 = mys.nextInt();



int num123,num223;



for(num123 = num1,num223 = num2;

num123< = num12 || num223< = num22;

num123 ++, num223 ++);

{

System.out.println(num123 * num223);

}





但该程序只给我一个价值



我尝试了什么:



我还试过这个:



for(num123 = num1,num223 = num2;

num123< = num12&& num223< = num22;

num123 ++,num223 ++);

{

System.out.println(num123 * num223);

}

Hi Guys

I want to write a program that multiplies two numbers in a range

here is my code :



Scanner mys = new Scanner(System.in);

System.out.println("1");
int num1 = mys.nextInt();

System.out.println("12");
int num12 = mys.nextInt();

System.out.println("2");
int num2 = mys.nextInt();

System.out.println("22");
int num22 = mys.nextInt();

int num123 , num223 ;

for(num123=num1 , num223=num2 ;
num123<=num12 || num223<=num22 ;
num123++ , num223++ );
{
System.out.println(num123 * num223);
}


But the program gives me just a value

What I have tried:

I also tried this :

for(num123=num1 , num223=num2 ;
num123<=num12 && num223<=num22 ;
num123++ , num223++ );
{
System.out.println(num123 * num223);
}

推荐答案

for(num123=num1 , num223=num2 ;num123<=num12 && num223<=num22 ;num123++ , num223++ );





它应该



it should be

for(num123=num1 , num223=num2 ;num123<=num12 && num223<=num22 ; num123++ , num223++ )
{
    System.out.println(num123 * num223);
} 





当前代码正在执行循环,完成循环后,它将执行println,它将始终打印出变量最后一个值的乘积。



What the current code doing is executing the loop, after finished the loop, it will then execute the println, which will always print out the products of the last value of the variables.


这篇关于Java:如何正确使用带扫描仪的循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 17:17