如何从用户那里获得输入并找到它们的最高,最低和平均值?

时间:2017-12-04 23:23:47

标签: java input average

我正在编写代码,询问用户的姓名,他们有多少工作,以及这些工作的收入。然后代码找到最高和最低的支付收入,以及他们输入的工作的平均值。

我遇到了最高和最低付费部分的问题,同时找到了平均值,同时仍然维护用户输入的内容,以便稍后背诵。

例: 输入:10000 30000 50000 “奥黛丽你好。你有3个工作岗位。薪水最高的工作支付5万美元。最低工资支付10000美元。输入工作的平均工资为30000美元

****继承了我编辑的代码,但它运行不正常。我相信它与int和double有关。我不确定哪个代码应该加倍,哪些代码应该是int。****

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class JobIncome {


public static void main(String[] args) throws FileNotFoundException {

Scanner input = new Scanner (System.in);
System.out.println("What is your first name? ");
String firstName = input.nextLine();

Scanner scan = new Scanner (System.in);
System.out.println("How many jobs have you had? ");
int jobNum = scan.nextInt();

//Declarations
int total = 0;
int average = 0;

//for loop asks for the incomes of the user's previous 
//jobs and stores them into an array
int arrayOfIncomes[] = new int[jobNum];
for(int i = 1; i <= arrayOfIncomes.length; i++){
    System.out.println("Enter the income of job #" + i + " : ");
    arrayOfIncomes[i] = scan.nextInt();
    total = total + arrayOfIncomes[i];
}

average = total/jobNum;

//Start of the code that will find the min and max
int min = arrayOfIncomes[0];
int max = arrayOfIncomes[0];

for (int i = 1; i < arrayOfIncomes.length; i++) {
    if (arrayOfIncomes[i] > max) {
        max = arrayOfIncomes[i];
    }
}

for (int i = 1; i < arrayOfIncomes.length; i++) {
    if (arrayOfIncomes[i] < min) {
        min = arrayOfIncomes[i];
    }
}

//Print statement that gives the user all their information
System.out.println("Hello, " + firstName + ". You have had" + jobNum + 
"jobs. The highest paying job paid $" + max + 
". The lowest paying job paid $" + min + 
". The average pay for the " + jobNum + "jobs entered is $" + average + ".");


//Prompt asking the user if they would like to print their info into a text file
System.out.println("Would you like to output your information into a text file, yes or no? ");
String yesNo = input.nextLine();
if(yesNo.equals("yes")){
    System.out.println("");
} else {
    System.out.println("Goodbye.");
}


//Output code
Scanner console = new Scanner(System.in);
System.out.print("Your output file: ");
String outputFileName = console.next();
PrintWriter out = new PrintWriter(outputFileName);


//This code prints the information into the output text file
out.print("Hello, " + firstName + ". You have had" + jobNum + 
"jobs. The highest paying job paid $" + max + 
". The lowest paying job paid $" + min + 
". The average pay for the " + jobNum + "jobs entered is $" + average + ".");

out.close();
}

2 个答案:

答案 0 :(得分:0)

创建一个int变量sum。在您从用户获得输入时添加总和,然后将总和与变量jobNum分开,同时将至少一个变量转换为加倍。

示例:

double ave = (double) sum / jobNum;

答案 1 :(得分:0)

通过实例化一个数组来修复代码,jobIncomes的长度等于作业数。您可以保持运行平均值,但在舍入时会丢失精度。您还需要实例化三个整数变量:total,max和min,每个变量都为零。

对于循环的每次迭代,您应该添加以下代码:

jobIncomes[i-1]=s.nextInt();
if (jobIncomes[i-1]<min)
{ min=jobIncomes[i-1];}
if (jobIncomes[i-1]>max
{max=jobIncomes[i-1];}
total+=jobIncomes[i-1];

打印平均值时,可以将公式平均值= total / jobNum。如果要在if语句中简单地使用i进行索引,您可能还希望将数组值更改为从0读取到jobNum-1。