需要在java中的某个点重新开始
我对java真的很陌生,只是一般编程而已。我想做一个简单的“故事游戏”。 我希望程序在注释“如果walk.equals(“b”)(第二次)”的地方重新启动 这是我的密码: 如果写得不好,我很抱歉需要在java中的某个点重新开始,java,loops,Java,Loops,我对java真的很陌生,只是一般编程而已。我想做一个简单的“故事游戏”。 我希望程序在注释“如果walk.equals(“b”)(第二次)”的地方重新启动 这是我的密码: 如果写得不好,我很抱歉 import java.util.*; public class leikur1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.p
import java.util.*;
public class leikur1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("|---------Welcome to the adventure------------|");
System.out.println("Please enter Name"); // inputs name
String nafn = scan.nextLine();
nafn = nafn.toLowerCase();
System.out.println("Gender, male or female");
String kyn = scan.nextLine();
kyn = kyn.toLowerCase();
while((!kyn.equals("male")) && (!kyn.equals("female")) ) //bara haegt ad velja male eda female
{
System.out.println("That is not valid input" );
kyn = scan.nextLine();
kyn = kyn.toLowerCase();
}
System.out.println("Are you ready for the adventure");
String leikur = scan.nextLine();
leikur = leikur.toLowerCase();
while((!leikur.equals("yes")) && (!leikur.equals("no")) ) //impossible to input something else than male or female
{
System.out.println("That is not valid input" );
leikur = scan.nextLine();
leikur = leikur.toLowerCase();
}
if(leikur.equals("no"))
{
System.out.println("Thank you anyway"); //if input = no program ends
}
// if input = yes the game begins
else
{
System.out.println("Write Start to begin or Quit to exit");
String start = scan.nextLine();
start = start.toLowerCase();
String gender;
if(kyn.equals("male"))
{
gender = "he";
}
else
{
gender = "she";
}
while((!start.equals("start")) && (!start.equals("quit")) )
{
System.out.println("That is not valid input" );
start = scan.nextLine();
start = start.toLowerCase();
}
if(start.equals("start"))
{
System.out.println("Walking instructions: left - l right - r forward - f back - b down - d up - u\n");
System.out.println(nafn + " is in a abandoned house late at night, stuck in the basement with no light.\n");
System.out.println("In wich way should " + gender + " go to find his way to the stairs?" ); **// starting again point if walk.equals("b") (second time)**
String walk = scan.nextLine();
walk = walk.toLowerCase();
//////////
while((!walk.equals("f")) && (!walk.equals("b")) ) // not possible bcus of walls or no stairs
{
System.out.println("That is not possible" );
walk = scan.nextLine();
}
if(walk.equals("f"))
{
System.out.println("Great choice, " + gender + " found the stairs right away. Should"+gender+" go upstairs or go back?" );
}
else if (walk.equals("b"))
{
System.out.println("Oh boy! " + nafn +" got stuck in a beartrap and died... GAME OVER" );
}
/////////
walk = scan.nextLine();
while(!walk.equals("u") && !walk.equals("b") )
{
System.out.println("That is not possible" );
walk = scan.nextLine();
}
if(walk.equals("u"))
{
System.out.println("Excelent!" + nafn + " is now upstairs" );
}
**else if(walk.equals("b") )
{
System.out.println("Now " + nafn + " is at stage 1 again" );
}**
//////////////
// code 4 the game should be here above
}
else if(start.equals("quit"))
{
System.out.println("Thank you anyway");
}
}
}
}
如果您只想重复您的内容,一个
while
循环就足够了
boolean continue = true;
while(continue)
{
else if(walk.equals("b") )
{
System.out.println("Now " + nafn + " is at stage 1 again" );
continue = true;
}
else if(start.equals("quit"))
{
System.out.println("Thank you anyway");
continue = false;
}
}
我使用变量
continue
的原因是为了更好地说明while循环。您还可以使用while(true)
,这通常会永远循环。要跳到下一次迭代的开始,请使用continue
要中断,可以使用break
while(true)
{
else if(walk.equals("b") )
{
System.out.println("Now " + nafn + " is at stage 1 again" );
continue;
}
else if(start.equals("quit"))
{
System.out.println("Thank you anyway");
break;
}
}
如果你真的想把你的编码提升到一个新的水平,你可以采取一种更加数据驱动的方法,并有一个数据模型来定义你的游戏。以下是xml中的示例数据模型
<Places>
<Place name="YourRoom" text="You are in your room. Where would you like to go?">
<Option text="Enter the hallway." result="Hallway"></Option>
</Place>
<Place name="Hallway" text="You are in the hallway. Where would you like to go?">
<Option text="Go to your room" result="YourRoom"></Option>
</Place>
</Places>
然后你会写你的程序像这样
伪代码:
xmlElement currentRoom = // get starting element
while(true)
{
print(currentRoom.Attributes[text])
for(int i=0; i<currentRoom.Elements.length; i++)
{
print(currentRoom.Elements[i] + "type " + i+1;
}
int choice = getInt();
int result = currentRoom.Elements[choice].result;
currentRoom GetElementWithName(result);
}
xmlElement currentRoom=//获取起始元素
while(true)
{
打印(currentRoom.Attributes[文本])
对于(int i=0;i如果您只想重复您的内容,那么一个while
循环就足够了
boolean continue = true;
while(continue)
{
else if(walk.equals("b") )
{
System.out.println("Now " + nafn + " is at stage 1 again" );
continue = true;
}
else if(start.equals("quit"))
{
System.out.println("Thank you anyway");
continue = false;
}
}
我使用变量continue
的原因是为了更好地说明while循环。您也可以使用while(true)
,它通常会永远循环。要跳到下一次迭代的开始,请使用continue;
并中断,您可以使用break
while(true)
{
else if(walk.equals("b") )
{
System.out.println("Now " + nafn + " is at stage 1 again" );
continue;
}
else if(start.equals("quit"))
{
System.out.println("Thank you anyway");
break;
}
}
如果你真的想把你的编码提升到一个新的水平,你可以采取一种更数据驱动的方法,用一个数据模型来定义你的游戏
<Places>
<Place name="YourRoom" text="You are in your room. Where would you like to go?">
<Option text="Enter the hallway." result="Hallway"></Option>
</Place>
<Place name="Hallway" text="You are in the hallway. Where would you like to go?">
<Option text="Go to your room" result="YourRoom"></Option>
</Place>
</Places>
然后你会写你的程序像这样
伪代码:
xmlElement currentRoom = // get starting element
while(true)
{
print(currentRoom.Attributes[text])
for(int i=0; i<currentRoom.Elements.length; i++)
{
print(currentRoom.Elements[i] + "type " + i+1;
}
int choice = getInt();
int result = currentRoom.Elements[choice].result;
currentRoom GetElementWithName(result);
}
xmlElement currentRoom=//获取起始元素
while(true)
{
打印(currentRoom.Attributes[文本])
对于(int i=0;i如果您只想重复您的内容,那么一个while
循环就足够了
boolean continue = true;
while(continue)
{
else if(walk.equals("b") )
{
System.out.println("Now " + nafn + " is at stage 1 again" );
continue = true;
}
else if(start.equals("quit"))
{
System.out.println("Thank you anyway");
continue = false;
}
}
我使用变量continue
的原因是为了更好地说明while循环。您也可以使用while(true)
,它通常会永远循环。要跳到下一次迭代的开始,请使用continue;
并中断,您可以使用break
while(true)
{
else if(walk.equals("b") )
{
System.out.println("Now " + nafn + " is at stage 1 again" );
continue;
}
else if(start.equals("quit"))
{
System.out.println("Thank you anyway");
break;
}
}
如果你真的想把你的编码提升到一个新的水平,你可以采取一种更数据驱动的方法,用一个数据模型来定义你的游戏
<Places>
<Place name="YourRoom" text="You are in your room. Where would you like to go?">
<Option text="Enter the hallway." result="Hallway"></Option>
</Place>
<Place name="Hallway" text="You are in the hallway. Where would you like to go?">
<Option text="Go to your room" result="YourRoom"></Option>
</Place>
</Places>
然后你会写你的程序像这样
伪代码:
xmlElement currentRoom = // get starting element
while(true)
{
print(currentRoom.Attributes[text])
for(int i=0; i<currentRoom.Elements.length; i++)
{
print(currentRoom.Elements[i] + "type " + i+1;
}
int choice = getInt();
int result = currentRoom.Elements[choice].result;
currentRoom GetElementWithName(result);
}
xmlElement currentRoom=//获取起始元素
while(true)
{
打印(currentRoom.Attributes[文本])
对于(int i=0;i如果您只想重复您的内容,那么一个while
循环就足够了
boolean continue = true;
while(continue)
{
else if(walk.equals("b") )
{
System.out.println("Now " + nafn + " is at stage 1 again" );
continue = true;
}
else if(start.equals("quit"))
{
System.out.println("Thank you anyway");
continue = false;
}
}
我使用变量continue
的原因是为了更好地说明while循环。您也可以使用while(true)
,它通常会永远循环。要跳到下一次迭代的开始,请使用continue;
并中断,您可以使用break
while(true)
{
else if(walk.equals("b") )
{
System.out.println("Now " + nafn + " is at stage 1 again" );
continue;
}
else if(start.equals("quit"))
{
System.out.println("Thank you anyway");
break;
}
}
如果你真的想把你的编码提升到一个新的水平,你可以采取一种更数据驱动的方法,用一个数据模型来定义你的游戏
<Places>
<Place name="YourRoom" text="You are in your room. Where would you like to go?">
<Option text="Enter the hallway." result="Hallway"></Option>
</Place>
<Place name="Hallway" text="You are in the hallway. Where would you like to go?">
<Option text="Go to your room" result="YourRoom"></Option>
</Place>
</Places>
然后你会写你的程序像这样
伪代码:
xmlElement currentRoom = // get starting element
while(true)
{
print(currentRoom.Attributes[text])
for(int i=0; i<currentRoom.Elements.length; i++)
{
print(currentRoom.Elements[i] + "type " + i+1;
}
int choice = getInt();
int result = currentRoom.Elements[choice].result;
currentRoom GetElementWithName(result);
}
xmlElement currentRoom=//获取起始元素
while(true)
{
打印(currentRoom.Attributes[文本])
对于(int i=0;i你应该学习Java、OOP和其他基本结构。如果你真的坚持,你可以使用(老实说,自从结构化编程出现以来,在60年代人们尝试不再使用GOTO…但是如果你想面试失败,那就去吧;-))
旁注:它很有魅力,但请不要这样做(或者至少不要说我是告诉你标签的人)!你应该学习Java、OOP和其他基本构造。如果你真的坚持,你可以使用(老实说,自从结构化编程出现以来,在60年代,人们尝试不再使用GOTO…但是如果你想面试失败,那就去吧;-))
旁注:它很有魅力,但请不要这样做(或者至少不要说我是告诉你标签的人)!你应该学习Java、OOP和其他基本构造。如果你真的坚持,你可以使用(老实说,自从结构化编程出现以来,在60年代,人们尝试不再使用GOTO…但是如果你想面试失败,那就去吧;-))
旁注:它很有魅力,但请不要这样做(或者至少不要说我是告诉你标签的人)!你应该学习Java、OOP和其他基本构造。如果你真的坚持,你可以使用(老实说,自从结构化编程出现以来,在60年代,人们尝试不再使用GOTO…但是如果你想面试失败,那就去吧;-))
旁注:它就像一个符咒,但请不要这样做(或者至少不要说我是那个告诉你标签的人)!试着给出简单的例子?试着缩小到你的问题的范围试着给出简单的例子?试着缩小到你的问题的范围试着给出简单的例子?试着缩小到你的问题的范围试着给出简单的例子?试着缩小到你的问题的范围,,如果你只想重复你的内容,一个while循环就足够了对不起,我说的不够清楚,我的意思是如果(walk.equals(“b”)),那么我想回到问题的开头