我想得到所有的num值并打印到(......)但我无法做到这一点。你可以帮帮我吗?
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
int operands,num;
int q=1;
int a=0;
do
{
System.out.println("Enter the number of operands (in range 2-10):");
operands=keyboard.nextInt();
} while ((operands<2) || (operands>10));
for (int number=1; number<=operands; number++)
{
System.out.println("Enter number "+number+":");
num=keyboard.nextInt();
q=q*num;
}
System.out.print("Multiplication of numbers "+(.......)+" is: "+q);
答案 0 :(得分:2)
我喜欢FranzKnut的回答,但如果性能问题,即使它不是,那么请考虑使用字符串生成器。
在使用循环之前
StringBuilder sb = new StringBuilder("my numbers are: ");
在循环内添加以下代码。
sb.append(num);
然后在循环结束时你有类似
的东西System.out.println(sb.toString());
答案 1 :(得分:1)
使用在循环之前声明的其他String变量:
String numbers=" ";
并在循环体中添加它:
numbers += num+" ";
然后在(.......)
的位置打印出这个字符串答案 2 :(得分:0)
只需在程序中添加一个String,然后使用新添加的数字进行连接。最后打印您必须打印的字符串。
t1
答案 3 :(得分:0)
好的,你有
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Numbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
int operands,num;
int q=1;
int a=0;
List<Integer> numbers = new ArrayList<Integer>();
int newNumbers = 1;
do
{
System.out.println("Enter the number of operands (in range 2-10):");
operands=keyboard.nextInt();
} while ((operands<2) || (operands>10));
for (int number=1; number<=operands; number++)
{
System.out.println("Enter number "+number+":");
num=keyboard.nextInt();
q=q*num;
numbers.add(num);
}
StringBuilder newTextNumber = new StringBuilder("");
for(Integer s: numbers){
newTextNumber.append(s).append(" ");
newNumbers *= s;
}
System.out.print("Multiplication of numbers "+newTextNumber+" is: "+newNumbers);
}
}
Enter the number of operands (in range 2-10):
3
Enter number 1:
20
Enter number 2:
30
Enter number 3:
10
Multiplication of numbers 20 30 10 is: 6000