大家好我基本上创建了一个程序,它接受来自用户的大约5个输入并将它们存储在一个名为course的数组中....每次用户输入1个课程我想询问用户是否要继续..如果他键入是程序继续,否则它显示数组中的值(课程)......
我遇到问题: -
1.i如果他已经输入了第5个最后一个值,我不想让用户继续。
2.我的第二个循环基本上被忽略了我不知道为什么如果它起作用,每件事都会好的。
3.如果用户输入最后一个值并按照我的程序按是,它仍然不会在我的课程(数组)中打印这些值。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ArrayExample;
import java.util.Scanner;
/**
*
* @author jodh_
*/
public class ArrayExample {
public static void main(String args[]) {
String[] course = new String[5];
for (int i = 0; i < 5; i++) {
System.out.println("Please enter course name " + (i + 1) + ": ");
Scanner gettingname = new Scanner(System.in);
String coursenames = gettingname.nextLine();
course[i] = coursenames;
if (i < 4) {
for(int j =0; j<4; j++){
System.out.println("");
System.out.println("Would you like to continue? ((y)Yes / (n)No)");
Scanner gettingYesOrNo = new Scanner(System.in);
String input = gettingYesOrNo.nextLine();
if (input.equals("yes") || input.equals("y") || input.equals("Y") || input.equals("YES") || input.equals("Yes")) {
System.out.println("");
break;
} else if (input.isEmpty()) {
System.out.println("Input cannot be empty!! Please try again");
} else if (input.equals("no") || input.equals("No") || input.equals("NO") || input.equals("n") || input.equals("N")) {
System.out.println("Your select courses are as follows");
}
for (int x = 0; x < i + 1; x++) {
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
System.exit(0);
}
}
}
}
}
答案 0 :(得分:1)
如果您将break
更改为continue
,这将正常工作。请尝试使用我的代码。
String[] course = new String[5];
for (int i = 0 ; i < 5 ; i++){
System.out.println("Please enter course name " + (i + 1) + ": ");
Scanner gettingname = new Scanner(System.in);
String coursenames = gettingname.nextLine();
course[i] = coursenames;
if (i < 4){
System.out.println("");
System.out.println("Would you like to continue? ((y)Yes / (n)No)");
Scanner gettingYesOrNo = new Scanner(System.in);
input = gettingYesOrNo.nextLine();
if (input.equals("yes") || input.equals("y") || input.equals("Y") || input.equals("YES") || input.equals("Yes")) {
System.out.println("");
continue;
} else if (input.isEmpty()) {
System.out.println("Input cannot be empty!! Please try again");
} else if (input.equals("no") || input.equals("No") || input.equals("NO") || input.equals("n") || input.equals("N")) {
System.out.println("Your select courses are as follows");
}
}
for (int x = 0; x < i + 1; x++) {
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
}
}
这是输出
Please enter course name 1:
androiid
Would you like to continue? ((y)Yes / (n)No)
y
Please enter course name 2:
dsfnfv
Would you like to continue? ((y)Yes / (n)No)
y
Please enter course name 3:
sfgngff
Would you like to continue? ((y)Yes / (n)No)
y
Please enter course name 4:
grgngr
Would you like to continue? ((y)Yes / (n)No)
y
Please enter course name 5:
fgmt
1: androiid
2: dsfnfv
3: sfgngff
4: grgngr
5: fgmt
Process finished with exit code 0
答案 1 :(得分:1)
我在您的代码中做了很多更改,比如删除一些不必要的循环,但它现在正在工作
public static void main(String args[]) {
String[] course = new String[5];
Scanner gettingname = new Scanner(System.in);
int i=0;
for (; i < 5; i++) {
System.out.println("Please enter course name " + (i + 1) + ": ");
course[i] =gettingname.nextLine();
System.out.println("");
if(i==4) break;
System.out.println("Would you like to continue? ((y)Yes / (n)No)");
Scanner gettingYesOrNo = new Scanner(System.in);
String input = gettingYesOrNo.nextLine();
if (input.equals("yes") || input.equals("y") || input.equals("Y") || input.equals("YES") || input.equals("Yes")) {
System.out.println("");
continue;
}
else if (input.isEmpty()) {
System.out.println("Input cannot be empty!! Please try again");
}
else if (input.equals("no") || input.equals("No") || input.equals("NO") || input.equals("n") || input.equals("N")) {
break;
}
}
System.out.println("Your select courses are as follows");
for (int x = 0; x < i+1 ; x++) {
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
}
}
答案 2 :(得分:0)
以下是我编写的一些可能修复程序的代码。你必须实现其余的,但它应该很容易。
import java.util.Scanner;
public class ArrayExample {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] course = new String[5];
String yesOrNo;
int index = 0;
do{
System.out.println("Please enter course name " + (index + 1) + ": ");
course[index] = input.nextLine();
if(index < 4) {
System.out.println("Would you like to continue? ((y)Yes / (n)No)");
yesOrNo = input.nextLine();
}
index++;
}while(yesOrNo.toLowerCase().startsWith("y) && index < 5)
//if the loop is exited then we know either the user chose not to continue or all the course names have been filled
//handle that here
}
}
我所做的逻辑:
while循环条件使得在填充所有课程时不会继续循环。在循环内部,它检查索引是否不是4。
不再需要你的第二个循环
您现在可以在循环后打印数组。
制作两个扫描仪完全没用,占用内存,因为你甚至没有关闭它们。
我希望这会有所帮助。
答案 3 :(得分:0)
这就是代码格式化非常重要的原因!嵌套if / else if / for语句的间距使您可以更快,更轻松地遵循程序的逻辑。
以下是代码的格式化版本:
import java.util.Scanner;
/** * * @author jodh_ */
public class ArrayExample
{
public static void main(String args[])
{
String[] course = new String[5];
for (int i = 0; i < 5; i++)
{
System.out.println("Please enter course name " + (i + 1) + ": ");
Scanner gettingname = new Scanner(System.in);
String coursenames = gettingname.nextLine();
course[i] = coursenames;
if (i < 4)
{
for(int j =0; j<4; j++)
{
System.out.println("");
System.out.println("Would you like to continue? ((y)Yes / (n)No)");
Scanner gettingYesOrNo = new Scanner(System.in);
String input = gettingYesOrNo.nextLine();
if (input.equals("yes") || input.equals("y") || input.equals("Y") || input.equals("YES") || input.equals("Yes"))
{
System.out.println("");
break;
}
else if (input.isEmpty())
{
System.out.println("Input cannot be empty!! Please try again");
}
else if (input.equals("no") || input.equals("No") || input.equals("NO") || input.equals("n") || input.equals("N"))
{
System.out.println("Your select courses are as follows");
}
for (int x = 0; x < i + 1; x++)
{
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
System.exit(0);
}
}
}
}
}
在这个格式化的代码中,我们可以很容易地看到每个代码的执行位置。
如果您查看检查“是”的部分,您会看到如果满足条件,它会突破内部for循环。这意味着如果用户给出“是”的答案,则不会执行内部循环中此分隔下的所有代码。
if (input.equals("yes") || input.equals("y") || input.equals("Y") || input.equals("YES") || input.equals("Yes"))
{
System.out.println("");
break;
}
你的print语句在这个内部for循环中,这实际上意味着它只会在不满足“yes”if语句的条件时执行。如果你要在“你想继续吗?”中输入文字“cat”。提示它将跳过所有if / else if语句,因为它们都没有被满足,执行print循环,输出课程和System.exit(0);终止程序。
如果用户输入“否”,则预期目的是输出课程。因此,让我们看看是否可以将打印循环移动到“否”中。
else if (input.equals("no") || input.equals("No") || input.equals("NO") || input.equals("n") || input.equals("N"))
{
System.out.println("Your select courses are as follows");
for (int x = 0; x < i + 1; x++)
{
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
System.exit(0);
}
仅当用户输入“否”时才会输出课程。然后它将终止该计划。完美!
所以让我们谈谈这个空输入条件:
else if (input.isEmpty())
{
System.out.println("Input cannot be empty!! Please try again");
}
除非您需要专门检查输入是否为空,否则您可能只想用else语句替换它。让我们看看它会是什么样子。
if (input.equals("yes") || input.equals("y") || input.equals("Y") || input.equals("YES") || input.equals("Yes"))
{
System.out.println("");
break;
}
else if (input.equals("no") || input.equals("No") || input.equals("NO") || input.equals("n") || input.equals("N"))
{
System.out.println("Your select courses are as follows");
for (int x = 0; x < i + 1; x++)
{
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
System.exit(0);
}
else
{
System.out.println("Invalid input!! Please try again.");
j--;
}
你需要减少j,因为在for循环结束时它将向前递增到下一个过程。由于用户输入无效,我们希望将路线迭代器保持在原位,并继续询问用户是否愿意继续进行有效选择。
我们快到了!
现在我们已经对输入条件进行了排序,当用户到达第5道菜时会发生什么?
现在,如果用户回答“否”,我们只打印课程。因此,在课程循环结束时,我们需要打印完整的课程字符串数组。最终产品如下:
import java.util.Scanner;
/** * * @author jodh_ */
public class ArrayExample
{
public static void main(String args[])
{
String[] course = new String[5];
for (int i = 0; i < 5; i++)
{
System.out.println("Please enter course name " + (i + 1) + ": ");
Scanner gettingname = new Scanner(System.in);
String coursenames = gettingname.nextLine();
course[i] = coursenames;
if (i < 4)
{
for(int j =0; j<4; j++)
{
System.out.println("");
System.out.println("Would you like to continue? ((y)Yes / (n)No)");
Scanner gettingYesOrNo = new Scanner(System.in);
String input = gettingYesOrNo.nextLine();
if (input.equals("yes") || input.equals("y") || input.equals("Y") || input.equals("YES") || input.equals("Yes"))
{
System.out.println("");
break;
}
else if (input.equals("no") || input.equals("No") || input.equals("NO") || input.equals("n") || input.equals("N"))
{
System.out.println("Your select courses are as follows");
for (int x = 0; x < i + 1; x++)
{
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
System.exit(0);
}
else
{
System.out.println("Invalid input!! Please try again");
j--;
}
}
}
}
System.out.println("Your select courses are as follows");
for (int x = 0; x < 5; x++)
{
System.out.println("");
System.out.println((x + 1) + ": " + course[x]);
}
}
}
注意:有更多最佳方法可以获得相同的结果,但我尽量使代码尽可能接近原始逻辑。