java 输入多个数字
Java中的Scanner类如何用循环实现输入多个数字啊???我是个初学者各位大哥大姐前辈们帮帮忙啊importjava.util.*;publicclassjiafa{...
Java 中的Scanner类
如何用循环实现输入多个数字啊???
我是个初学者 各位大哥大姐 前辈们帮帮忙啊
import java.util.*;
public class jiafa
{
public static void main(String[] arg)
{
int i;
System.out.print("请输入数值number1和number2 :");
for(i=1;i<=2;i++)
{
Scanner number=new Scanner(System.in);
number1=number.nextInt();
number2=number.nextInt();
}
int number1,number2;
number2=number1+number2;
System.out.println("number2 is " +number2);
}
}
下面这个是一个一个输入的没有用循环
import java.util.*;
public class jiafa1
{
public static void main(String[] arg)
{
System.out.print("请输入数值number1和number2 :");
int number1,number2;
Scanner number=new Scanner(System.in);
number1=number.nextInt();
Scanner number0=new Scanner(System.in);
number2=number0.nextInt();
number2=number1+number2;
System.out.println("number2 is: "+number2 );
}
} 展开
如何用循环实现输入多个数字啊???
我是个初学者 各位大哥大姐 前辈们帮帮忙啊
import java.util.*;
public class jiafa
{
public static void main(String[] arg)
{
int i;
System.out.print("请输入数值number1和number2 :");
for(i=1;i<=2;i++)
{
Scanner number=new Scanner(System.in);
number1=number.nextInt();
number2=number.nextInt();
}
int number1,number2;
number2=number1+number2;
System.out.println("number2 is " +number2);
}
}
下面这个是一个一个输入的没有用循环
import java.util.*;
public class jiafa1
{
public static void main(String[] arg)
{
System.out.print("请输入数值number1和number2 :");
int number1,number2;
Scanner number=new Scanner(System.in);
number1=number.nextInt();
Scanner number0=new Scanner(System.in);
number2=number0.nextInt();
number2=number1+number2;
System.out.println("number2 is: "+number2 );
}
} 展开
2个回答
展开全部
import java.util.Scanner;
public class NumReader {
private static Scanner san = new Scanner(System.in);
public static void main(String[] args) {
int len = 0;
System.out.print("你想输入多少个数字: ");
len = san.nextInt();
int [] numbers = new int[len];
for(int i = 0; i < len; i ++) {
System.out.print("请输入第" + (i + 1) + "个数字: ");
numbers[i] = san.nextInt();
}
System.out.print("你输入的" + len +"数字分别是: ");
for(int i: numbers) {
System.out.print(i + " ");
}
}
public class NumReader {
private static Scanner san = new Scanner(System.in);
public static void main(String[] args) {
int len = 0;
System.out.print("你想输入多少个数字: ");
len = san.nextInt();
int [] numbers = new int[len];
for(int i = 0; i < len; i ++) {
System.out.print("请输入第" + (i + 1) + "个数字: ");
numbers[i] = san.nextInt();
}
System.out.print("你输入的" + len +"数字分别是: ");
for(int i: numbers) {
System.out.print(i + " ");
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Scanner;
public class TestInPut {
public static void main(String[] args) {
Scanner c=new Scanner(System.in);
int[] i=new int[2];
for(int j=0;j<2;j++){
i[j]=c.nextInt();
}
System.out.println(i[0]+i[1]);
}
}
public class TestInPut {
public static void main(String[] args) {
Scanner c=new Scanner(System.in);
int[] i=new int[2];
for(int j=0;j<2;j++){
i[j]=c.nextInt();
}
System.out.println(i[0]+i[1]);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |