java for循环输入_Java - 使用for循环读取用户输入

在Java中,我无法使用单个用户输入整数序列运行多个循环。个别地,他们单独运行,但一起打印出不正确的数字。Java - 使用for循环读取用户输入

我不知道是什么原因导致此问题。

这是我的代码。

import java.util.Scanner;

public class SequenceTester

{

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

System.out.println("Enter a sequence of integers. " +

"Enter a non-integer to terminate");

int sequence = in.nextInt();

//Print One

int min = sequence;

while(in.hasNextInt())

{

int input = in.nextInt();

if(input < smallest)

{

smallest = input;

}

}

System.out.println(smallest);

//Print Two

int max = sequence;

while(in.hasNextInt())

{

int input = in.nextInt();

if(input > max)

{

max = input;

}

}

System.out.println(max);

//Print Three

int even = 0;

int odd = 0;

while(in.hasNextInt())

{

int input = in.nextInt();

if((input %2) == 0)

{

even++;

}

else

{

odd++;

}

}

System.out.println(even);

System.out.println(odd);

//Print Four

double total = 0;

int count = 0;

while (in.hasNextInt())

{

Int input = in.nextInt();

total = total + input;

count++;

}

double average = 0;

if (count > 0)

{

average = total/count;

}

System.out.println(average);

}

}

+1

我喜欢这种“循环”现在是一种算法。 –

2013-03-19 19:17:34

+0

打印不正确的数字如何?输出是什么?你期望它是什么? –

2013-03-19 19:17:43

+1

另外:“单独跑步”和“一起跑步”是什么意思? –

2013-03-19 19:18:05

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值