请帮我优化代码并写出 class setbox { static int number11; static int number12; static int number13; static int number14; Scanner h = new Scanner(System.in); static int setbox1() { //设置总量 Scanner h = new Scanner(System.in); int number11 = h.nextInt(); if (number11 < 3) { System.out.println("输入错误"); System.out.println("输入的数量应该>=3"); } return number11; } static int setbox2() { //设置一等奖的个数 Scanner h = new Scanner(System.in); int number12 = h.nextInt(); if (number12 <= number11) { System.out.println("输入错误"); System.out.println("数量多于总量"); } return number12; } static int setbox3() { Scanner h = new Scanner(System.in); int number13 = h.nextInt(); if (number13 <= number11 - number12) { System.out.println("输入错误"); System.out.println("数量多于总量"); } return number13; } static int setbox4() { Scanner h = new Scanner(System.in); int number14 = h.nextInt(); if (number14 <= number11 - number12 - number13) { System.out.println("输入错误"); System.out.println("数量多于总量"); } return number14; } }
时间: 2023-03-31 13:02:41 浏览: 91
BOX_长方体面积_
5星 · 资源好评率100%
好的,我会帮您优化代码并写出 class setbox。以下是优化后的代码:
import java.util.Scanner;
public class setbox {
private int number11;
private int number12;
private int number13;
private int number14;
public setbox() {
Scanner h = new Scanner(System.in);
System.out.println("请输入总量:");
this.number11 = h.nextInt();
while (this.number11 < 3) {
System.out.println("输入错误,请重新输入:");
this.number11 = h.nextInt();
}
System.out.println("请输入一等奖的个数:");
this.number12 = h.nextInt();
while (this.number12 > this.number11) {
System.out.println("输入错误,请重新输入:");
this.number12 = h.nextInt();
}
System.out.println("请输入二等奖的个数:");
this.number13 = h.nextInt();
while (this.number13 > this.number11 - this.number12) {
System.out.println("输入错误,请重新输入:");
this.number13 = h.nextInt();
}
System.out.println("请输入三等奖的个数:");
this.number14 = h.nextInt();
while (this.number14 > this.number11 - this.number12 - this.number13) {
System.out.println("输入错误,请重新输入:");
this.number14 = h.nextInt();
}
}
public int getNumber11() {
return number11;
}
public int getNumber12() {
return number12;
}
public int getNumber13() {
return number13;
}
public int getNumber14() {
return number14;
}
}
这个 class setbox 可以用来设置一个抽奖活动的奖项数量。您可以通过调用 getNumber11()、getNumber12()、getNumber13() 和 getNumber14() 方法来获取设置的总量、一等奖的个数、二等奖的个数和三等奖的个数。
阅读全文