现在的效果是输入工人数目后,输入相同数目的工作小时,最后计算总工资。
import java.util.Scanner;
public class SalaryCalculation {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int n,total = 0;
System.out.println("请输入工人数目:");
n = scanner.nextInt();
System.out.println("请输入工人的工作小时:");
for(int i = 0; i < n; i++){
total += 9 * scanner.nextInt();
}
System.out.println("总工资:" + total);
System.exit(0);
}
}
现在我想在工人数目和工人小时中间加一个输入工人名字的功能,但怎么加都有错误。
求高手帮忙修改下。
效果是输入工人数目后,输入相同数目的名字,最后输入相同数目的工作小时,最后计算总工资。
谢谢各位!
------解决方案--------------------
- Java code
package com;import java.util.Scanner;public class SalaryCalculation { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); int n,total = 0; String name; System.out.println("请输入工人数目:"); n = scanner.nextInt(); StringBuilder sBuilder = new StringBuilder(); for(int i = 0; i < n; i++){ System.out.println("请输入工人名称:"); name = scanner.next(); System.out.println("请输入工人的工作小时:"); int time = scanner.nextInt(); total += 9 * time; sBuilder.append(" 工人:" + name + "工作小时:" + time); } System.out.println(sBuilder.toString() + ",总工资:" + total); System.exit(0); }}
------解决方案--------------------
package chapter.section1;
import java.util.Scanner;
public class SalaryCalculation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n,total = 0;
String name = null;
System.out.print("请输入工人数目:");
n = scanner.nextInt();
int ss = 0;
for(int i = 0; i < n; i++){
System.out.println("请输入工人名称:");
name = scanner.next();
System.out.println("请输入工人的工作小时:");
int time = scanner.nextInt();
ss=time;
total += 9 * ss;
System.out.print(" 工人:" + name + "工作小时:" + time);
}
System.out.println(" 工人:" + name + "工作小时:" + ss + ",总工资:" + total);
System.exit(0);
}
}
------解决方案--------------------
package chapter.section1;
import java.util.Scanner;
public class SalaryCalculation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n,total = 0;
System.out.print("请输入工人数目:");
n = scanner.nextInt();
String [] name1 = new String [n];
StringBuilder sBuilder = new StringBuilder();
int ss = 0;
for(int i = 0; i < n; i++){
System.out.println("请输入工人名称:");
name1[i] = scanner.next();
System.out.println("请输入工人的工作小时:");
int time = scanner.nextInt();
ss=time;
total += 9 * ss;
System.out.println(" 工人:" + name1[i] + "工作小时:" + time);
}
for(int i=0;i<n;i++){
System.out.print(" 工人:" + name1[i] + "工作小时:" + ss);// + ",总工资:" + total);
}
System.out.println( ",总工资:" + total);