switch case 不起作用,而是忽略方法
switch case doesn't work, ignores methods instead
我正在尝试使用 switch 语句而不是一系列 if/elses,但是当我 运行 代码只有前两种情况 运行 正确而其他情况被忽略.我已经用 if 条件测试了这些方法并且它工作正常,但我想使用 switch 语句。 (一些方法我还没有得到,因此评论)
输入:A 25
输出:25 被添加到 Bag.
输入:F 25
输出:袋子里有 (1) 25。
输入:S
输出:(无)
预期输出:包中有 1 个数字。
输入:m
输出:(无)
预期输出:Bag 中的最小数量为 25.
import java.util.Scanner;
public class Bag {
int index = 0;
int[] array = new int[50];
public static void main(String[] args) {
int x = 0;
Bag bag = new Bag();
Scanner scan = new Scanner(System.in);
while (x == 0) {
System.out.print("Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> ");
char c = scan.next().charAt(0);
int y = scan.nextInt();
switch(c) {
case 'A': bag.Add(y);
break;
//case 'D': bag.Delete(y);
//break;
case 'F': bag.Find(y);
break;
case 'S': bag.Size();
break;
case 'm': bag.Min();
break;
case 'M': bag.Max();
break;
/*case 'L': bag.List();
break;
case 'Q': bag.Quit();*/
}
}
}
public void Add(int y) {
array[index] = y;
index++;
System.out.println(" " + y + " is added to the Bag. ");
}
public void Find(int y)
{
int count = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == y) {
count++;
}
}
System.out.println(" There is (" + count + ") " + y
+ " in the Bag.");
}
public void Size() {
System.out.println(" There are " + index + " numbers in the Bag.");
}
public void Min() {
int min = array[0];
for (int i = 1; i < index; i++) {
if(min > array[i]) {
min = array[i];
}
}
System.out.println(" The minimum number in the Bag is " + min + ".");
}
public void Max() {
int max = array[0];
for (int i = 1; i < index; i++) {
if(max < array[i]) {
max = array[i];
}
}
System.out.println(" The minimum number in the Bag is " + max + ".");
}
}
你总是读取一个整数,即使在不需要它们的情况下,如 "m"、"M" 或“S。如果你键入 M<return>
0<return>
,它会打印最大值等
看看你的代码。
char c = scan.next().charAt(0); //Here you first expect char (program waiting for a input)
int y = scan.nextInt(); //And here you expect int (program waiting for a input)
我觉得这样的东西应该有用。
System.out.print("Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> ");
char c = scan.next().charAt(0);
switch (c) {
case 'A':
int y = scan.nextInt(); // here you need next input
bag.Add(y);
break;
case 'F':
int y = scan.nextInt(); // here you need next input
bag.Find(y);
break;
case 'S':
bag.Size();
break;
case 'm':
bag.Min();
break;
case 'M':
bag.Max();
break;
}
您的问题在于您每次都调用 scan.nextInt()
,即使您选择的选项不需要第二个参数。
Scanner
方法如next()
、nextInt()
等,会一直挂起控制台等待你的输入。由于它始终需要两个输入,因此您缺少第二个参数。这就是为什么它没有打印任何东西。
while (x == 0) {
System.out.print("Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> ");
char c = scan.next().charAt(0);
int y = 0;
switch (c) {
case 'A':
y = scan.nextInt();
bag.Add(y);
break;
//case 'D': bag.Delete(y);
//break;
case 'F':
y = scan.nextInt();
bag.Find(y);
break;
case 'S':
bag.Size();
break;
case 'm':
bag.Min();
break;
case 'M':
bag.Max();
break;
}
}
我正在尝试使用 switch 语句而不是一系列 if/elses,但是当我 运行 代码只有前两种情况 运行 正确而其他情况被忽略.我已经用 if 条件测试了这些方法并且它工作正常,但我想使用 switch 语句。 (一些方法我还没有得到,因此评论)
输入:A 25
输出:25 被添加到 Bag.
输入:F 25
输出:袋子里有 (1) 25。
输入:S
输出:(无)
预期输出:包中有 1 个数字。
输入:m
输出:(无)
预期输出:Bag 中的最小数量为 25.
import java.util.Scanner;
public class Bag {
int index = 0;
int[] array = new int[50];
public static void main(String[] args) {
int x = 0;
Bag bag = new Bag();
Scanner scan = new Scanner(System.in);
while (x == 0) {
System.out.print("Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> ");
char c = scan.next().charAt(0);
int y = scan.nextInt();
switch(c) {
case 'A': bag.Add(y);
break;
//case 'D': bag.Delete(y);
//break;
case 'F': bag.Find(y);
break;
case 'S': bag.Size();
break;
case 'm': bag.Min();
break;
case 'M': bag.Max();
break;
/*case 'L': bag.List();
break;
case 'Q': bag.Quit();*/
}
}
}
public void Add(int y) {
array[index] = y;
index++;
System.out.println(" " + y + " is added to the Bag. ");
}
public void Find(int y)
{
int count = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == y) {
count++;
}
}
System.out.println(" There is (" + count + ") " + y
+ " in the Bag.");
}
public void Size() {
System.out.println(" There are " + index + " numbers in the Bag.");
}
public void Min() {
int min = array[0];
for (int i = 1; i < index; i++) {
if(min > array[i]) {
min = array[i];
}
}
System.out.println(" The minimum number in the Bag is " + min + ".");
}
public void Max() {
int max = array[0];
for (int i = 1; i < index; i++) {
if(max < array[i]) {
max = array[i];
}
}
System.out.println(" The minimum number in the Bag is " + max + ".");
}
}
你总是读取一个整数,即使在不需要它们的情况下,如 "m"、"M" 或“S。如果你键入 M<return>
0<return>
,它会打印最大值等
看看你的代码。
char c = scan.next().charAt(0); //Here you first expect char (program waiting for a input)
int y = scan.nextInt(); //And here you expect int (program waiting for a input)
我觉得这样的东西应该有用。
System.out.print("Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> ");
char c = scan.next().charAt(0);
switch (c) {
case 'A':
int y = scan.nextInt(); // here you need next input
bag.Add(y);
break;
case 'F':
int y = scan.nextInt(); // here you need next input
bag.Find(y);
break;
case 'S':
bag.Size();
break;
case 'm':
bag.Min();
break;
case 'M':
bag.Max();
break;
}
您的问题在于您每次都调用 scan.nextInt()
,即使您选择的选项不需要第二个参数。
Scanner
方法如next()
、nextInt()
等,会一直挂起控制台等待你的输入。由于它始终需要两个输入,因此您缺少第二个参数。这就是为什么它没有打印任何东西。
while (x == 0) {
System.out.print("Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> ");
char c = scan.next().charAt(0);
int y = 0;
switch (c) {
case 'A':
y = scan.nextInt();
bag.Add(y);
break;
//case 'D': bag.Delete(y);
//break;
case 'F':
y = scan.nextInt();
bag.Find(y);
break;
case 'S':
bag.Size();
break;
case 'm':
bag.Min();
break;
case 'M':
bag.Max();
break;
}
}