我是Java编程的新手(2周后),目前正在练习数组。
我的代码如下:
import java.util.Scanner;
public class LA2_2_1 {
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int x;
double avg;
double num = 0;
System.out.print("Enter the number of items: ");
x = scan.nextInt();
int [] numbers = new int [x];
double sum = 0;
sum = sum + num;
for (int index = 0; index < numbers.length; index++)
{
System.out.print("Enter the numbers: ");
num = scan.nextDouble();
sum = sum + num;
System.out.println(sum);
}
avg = sum/x;
System.out.println("The average is " + avg);
}
}
输出:
Enter the number of items: 10
Enter the numbers: 3.4 5 6 1 6.5 7.8 3.5 8.5 6.3 9.5
3.4
Enter the numbers: 8.4
Enter the numbers: 14.4
Enter the numbers: 15.4
Enter the numbers: 21.9
Enter the numbers: 29.7
Enter the numbers: 33.2
Enter the numbers: 41.7
Enter the numbers: 48.0
Enter the numbers: 57.5
The average is 5.75
我的问题是,如何格式化代码,以便"Enter the numbers"
只出现一次?我只是希望程序向用户询问他们想要输入的一系列数字。我不希望它输出多个“输入数字”。
我认为这可能与for循环中的输出有关,但我不是太积极。我尝试将其取出,“输入数字”的平均值和输出均转为0.0
。
任何输入都会有所帮助。
谢谢
答案 0 :(得分:0)
只需从
更改for
循环即可
for (int index = 0; index < numbers.length; index++)
{
System.out.print("Enter the numbers: ");
num = scan.nextDouble();
sum = sum + num;
System.out.println(sum);
}
到
System.out.print("Enter the numbers: ");
for (int index = 0; index < numbers.length; index++)
{
num = scan.nextDouble();
sum = sum + num;
}
System.out.println(sum);
答案 1 :(得分:0)
每次for
循环运行时,仔细查看代码,都会打印出
System.out.print("Enter the numbers: ");
设置数量的次数,在这种情况下为10。
为了解决此问题,您需要在System.out.print("Enter the numbers: ");
循环之前添加for
。
答案 2 :(得分:0)
它只是简单地将输入提示语句放在for循环外:
System.out.print("Enter the numbers: ");
for (int index = 0; index < numbers.length; index++)
{
num = scan.nextDouble();
sum = sum + num;
}
System.out.println(sum); // sum print statement outside loop to get total sum
答案 3 :(得分:0)
只需在循环外移动print语句即可。请尝试此工作链接http://rextester.com/live/ZDRYM2149
class Rextester
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int x;
double avg;
double num = 0;
System.out.print("Enter the number of items: ");
x = scan.nextInt();
int [] numbers = new int [x];
double sum = 0;
sum = sum + num;
System.out.print("Enter the numbers: ");
for (int index = 0; index < numbers.length; index++)
{
num = scan.nextDouble();
sum = sum + num;
}
System.out.println("The sum is " + sum);
avg = sum/x;
System.out.println("The average is " + avg);
}
}
答案 4 :(得分:0)
如果您真的在学习数组,请考虑我的答案,因为在您的代码中您没有使用数组
public static void main(String args[])
{
//sb object for getting the actual int
StringBuilder sb = new StringBuilder("");
Scanner scan = new Scanner(System.in);
int x;
//this is for the array element of numbers e.g. numbers[y]
int y=0;
double sum=0;
double avg;
System.out.print("Enter the number of items: ");
x = scan.nextInt();
//please add the scan.nextLine that is why you are getting
scan.nextLine();
double[] numbers=new double[x];
System.out.print("Enter the numbers followed by space:");
String numberEntered=scan.nextLine();
for(int i=0;i<numberEntered.length();++i)
{
//everytime loop iterate new sb with empty value is created
sb=new StringBuilder("");
while(i<numberEntered.length()&&numberEntered.charAt(i)!=' ')
{
//get the actual String integer not containing space
sb.append(numberEntered.charAt(i));
++i;
}
//convert the String actual integers to integer
numbers[y]=Double.parseDouble(sb.toString());
//increment y for the array element of numbers
++y;
}
//get the sum using foreach loop containing the array of numbers
for(double myNumbers:numbers)
{
sum+=myNumbers;
}
avg=sum/x;
System.out.println("The average is " + avg);
}