是否/循环卡死

时间:2018-12-10 21:23:01

标签: java do-while

我正在尝试使项目成为天际线的一部分,并且似乎无法解决此do / while循环问题。我觉得我一直在面对这个问题,却没有注意到。 问题:我运行该程序,它到达标题,并询问您是否要播放(y / n),并检查输入的键是“ y”还是“ n”,如果不是,则程序会再次询问您(y / n)。一切正常,但是当程序结束时,do / while循环似乎重新开始,并询问您是否要“播放(y / n)”。 任何帮助都将是惊人的。对我愚蠢的自我表示歉意。我是编码的新手,并认为这将是一个有趣的项目。 代码:

import java.util.*;

public class skyrim {

   public static void intro() {
      Scanner input = new Scanner(System.in);
System.out.println("The intro...");
      System.out.println("Continue... (x)");
      String B = input.next();
      if (B.startsWith("x"))   {
         for (int b = 0; b < 50; b++)  {
            System.out.println();
         }         
      }
      System.out.println("To the game...");
      System.out.println("Continue... (x)");
      String C = input.next();
      if (C.startsWith("x"))   {
         for (int c = 0; c < 50; c++)  {
            System.out.println();
         }         
      }
      System.out.println("That is definitely not Skyrim...");
      System.out.println("Continue... (x)");
      String D = input.next();
      if (D.startsWith("x"))   {
         for (int d = 0; d < 50; d++)  {
            System.out.println();
         }         
      }
      System.out.println("But actually totally is....");
      System.out.println("Continue... (x)");
      String E = input.next();
      if (E.startsWith("x"))   {
         for (int e = 0; e < 50; e++)  {
            System.out.println();
         }         
      }
      skyrimlogo();
      System.out.println("SKYRIM: The Quest of The Golden Claw");
      //System.out.println("*boom boomboom BOOM*");  
      System.out.println("Bethesda please don't sue me."); 
      String next = "";             
      do {
      System.out.println("Play (y/n)");
      next = input.next();
      if (next.startsWith("y"))   {
         awake(input);
      }
      if (next.startsWith("n"))   {
         System.out.println("Exiting Game...");
         System.exit(0);
      }
      }
      while (!next.startsWith("y") || !next.startsWith("n"));
      }


   public static void skyrimlogo()  {
         System.out.println("         /\\       /\\");
         System.out.println("        / (       ) \\");
         System.out.println("       / /\\)     (/\\ \\");
         System.out.println("      / /   (\\      \\ \\");
         System.out.println("     / /    ; \\_/)   \\ \\");       
         System.out.println("    / /    (,-. (     \\ \\");
         System.out.println("   / /         ) )     \\ \\");
         System.out.println("  / /       ,-','       \\ \\");         
         System.out.println(" / /     (\\(  ( /)       \\ \\");         
         System.out.println("/  '._____)\\)  \\/(______,'  \\");         
         System.out.println("\\                           /");
         System.out.println(" \\     .-.         ,-.     /");
         System.out.println("  \\   /   \\/\\   (\\/   \\   / ");
         System.out.println("   \\  \\      \\   )    /  /  ");
         System.out.println("    \\  \\      ) /   ,'  /  ");
         System.out.println("     \\  \\    / /   |   /");
         System.out.println("      \\  \\   \\ \\   |  /");
         System.out.println("       \\  )   ) )  | /");
         System.out.println("        \\ |  / /   (/");
         System.out.println("         \\) / /    ");
         System.out.println("           / /  /|");
         System.out.println("           \\ \\_/ )");
         System.out.println("            \\   / ");
         System.out.println("             \\_/ ");

   }



   public static void awake(Scanner input) {
      System.out.println("You awake to a cold morning on the back of a cart pulled by a pair of horses.");
      //wagon();
      System.out.println("Continue... (x)");
      String G = input.next();
      if (G.startsWith("x"))   {
         for (int g = 0; g < 7; g++)  {
            System.out.println();
         }         
      }
      System.out.println("RALOF: Hey, you there...");
      System.out.println("You're finally awake. You were trying to cross the border, right?");
      System.out.println("Walked right into that imperial ambush, same as us, and the thief over there.");
      System.out.println("Continue... (x)");
      String H = input.next();
      if (H.startsWith("x"))   {
         for (int h = 0; h < 7; h++)  {
            System.out.println();
         }         
      }
      System.out.println("LOKIR: Damn you Stormcloaks. Skyrim was fine until you came along. Empire was nice and lazy...");
      System.out.println("If they hadnt been looking for you, I could've stolen that horse and been halfway to Hammerfell.");
      System.out.println("You there. You and me -- we shouldnt be here. It's these Stormcloaks the Empire wants.");
      System.out.println("Continue... (x)");
      String I = input.next();
      if (I.startsWith("x"))   {
         for (int i = 0; i < 7; i++)  {
            System.out.println();
         }         
      }
      System.out.println("RALOF: We're all brothers and sisters in binds now, thief.");
      System.out.println("Continue... (x)");
      String J = input.next();
      if (J.startsWith("x"))   {
         for (int j = 0; j < 7; j++)  {
            System.out.println();
         }         
      } 
      System.out.println("You notice the man driving the cart to your left and that he is dressed in imperial armour.");
      System.out.println("He notices you eyeing him and hears Ralof and Lokir talking.");
      System.out.println("'Shut up back there!' he says");

      //mountains();
      //wagonWithSnow();

      System.out.println("Snow continues to fall as the cart you have awoken on makes its way through the snowy mountain passes...");
      System.out.println("Continue... (Press Any Key)");
      String K = input.next();
      if (K.startsWith("x"))   {
         for (int k = 0; k < 7; k++)  {
            System.out.println();
         }         
      }
      System.out.println("[Lokir looks at the gagged man.]");
      System.out.println("LOKIR: And what's wrong with him?");
      System.out.println("Continue... (x)");
      String L = input.next();
      if (L.startsWith("x"))   {
         for (int l = 0; l < 7; l++)  {
            System.out.println();
         }         
      }
      System.out.println("RALOF: Watch your tongue! You're speaking to Ulfric Stormcloak, the true High King.");
      System.out.println("LOKIR: Ulfric? The Jarl of Windhelm? You're the leader of the rebellion. But if they captured you..."); 
      System.out.println("Oh gods, where are they taking us?");
      System.out.println("RALOF: I don't know where we're going, but Sovngarde awaits.");
      System.out.println("LOKIR: No, this can't be happening. This isn't happening.");
      System.out.println("Continue... (x)");      
      String M = input.next();
      if (M.startsWith("x"))   {
         for (int m = 0; m < 7; m++)  {
            System.out.println();
         }         
      }
      System.out.println("RALOF: Hey, what village are you from, horse thief?");
      System.out.println("LOKIR: Why do you care?");
      System.out.println("RALOF: A Nord's last thoughts should be of home.");
      System.out.println("LOKIR: Rorikstead. I'm...I'm from Rorikstead.");
      System.out.println("Continue... (x)");      
      String N = input.next();
      if (N.startsWith("x"))   {
         for (int n = 0; n < 7; n++)  {
            System.out.println();
         }         
      }
      System.out.println("As the cart makes its way through the snowy mountain passes, they arrive at the gates of Helgan.\n");
      //HelganGate();
      System.out.println("Continue... (x)");      
      String R = input.next();
      if (R.startsWith("x"))   {
         for (int r = 0; r < 7; r++)  {
            System.out.println();
         }         
      }     
      System.out.println("A soldier calls out to the lead wagon,");
      System.out.println("Imperial Soldier: General Tullius, sir! The headsman is waiting!");
      System.out.println("General Tullius: Good. Let's get this over with.");
      System.out.println("Continue... (x)");
      String O = input.next();
      if (O.startsWith("x"))   {
         for (int o = 0; o < 7; o++)  {
            System.out.println();
         }         
      }
      System.out.println("LOKIR: Shor, Mara, Dibella, Kynareth, Akatosh. Divines, please help me!");
      System.out.println("RALOF: Look at him, General Tullius the Military Governor. And it looks like the"); 
      System.out.println("Thalmor are with him. Damn elves. I bet they had something to do with this.");
      System.out.println("RALOF: This is Helgen. I used to be sweet on a girl from here. Wonder if Vilod");
      System.out.println("is still making that mead with juniper berries mixed in. Funny...when I");
      System.out.println("was a boy, Imperial walls and towers used to make me feel so safe.");
      System.out.println("Continue... (x)");
      String P = input.next();
      if (P.startsWith("x"))   {
         for (int p = 0; p < 7; p++)  {
            System.out.println();
         }         
      }
      System.out.println("[A man and son watch the prisoners pull into town.]");
      System.out.println("HAMING (little boy): Who are they, daddy? Where are they going?");
      System.out.println("TOROLF (father): You need to go inside, little cub.");
      System.out.println("HAMING (little boy): Why? I want to watch the soldiers.");
      System.out.println("TOROLF (father): Inside the house. Now.");
      System.out.println("Continue... (x)");
      String Q = input.next();
      if (Q.startsWith("x"))   {
         for (int q = 0; q < 7; q++)  {
            System.out.println();
         }         
      }
      System.out.println("[The wagon stops near the chopping block.]");
      System.out.println("IMPERIAL SOLDIER: \"Whoa...WHOA...whoa\" he says to the horses pulling the carriage");
      System.out.println("LOKIR: Why are they stopping?");
      System.out.println("RALOF: Why do you think? End of the line. Let's go. Shouldn't keep the gods waiting for us.");
      System.out.println("LOKIR: No! Wait! We're not rebels!");
      System.out.println("RALOF: Face your death with some courage, thief.");
      System.out.println("Continue... (x)");
      String V = input.next();
      if (V.startsWith("x"))   {
         for (int v = 0; v < 7; v++)  {
            System.out.println();
         }         
      }      
      System.out.println("[Under the Imperials' watchful eye, the prisoners start jumping out.]");
      System.out.println("LOKIR: You've got to tell them! We weren't with you! This is a mistake!");
      System.out.println("IMPERIAL CAPTAIN: Step toward the block when we call your name. One at a time!");
      System.out.println("RALOF: Empire loves their damn lists.");
      System.out.println("Continue... (x)");
      String S = input.next();
      if (S.startsWith("x"))   {
         for (int s = 0; s < 7; s++)  {
            System.out.println();
         }         
      } 
      System.out.println("HADVAR: Ulfric Stormcloak. Jarl of Windhelm.");
      System.out.println("RALOF: It has been an honor, Jarl Ulfric!");
      System.out.println("Continue... (x)");
      String T = input.next();
      if (T.startsWith("x"))   {
         for (int t = 0; t < 7; t++)  {
            System.out.println();
         }         
      } 
      System.out.println("HADVAR: Ralof of Riverwood. Lokir of Rorikstead.");
      System.out.println("LOKIR: No, I'm not a rebel! You can't do this!");
      System.out.println("[He makes a break for it.]");
      System.out.println("IMPERIAL CAPTAIN: Halt!");
      System.out.println("LOKIR: You're not going to kill me!");
      System.out.println("IMPERIAL CAPTAIN: Archers!");
      System.out.println("[Lokir is downed with one bowshot.]");
      //archer();
      System.out.println("IMPERIAL CAPTAIN: Anyone else feel like running?");
      System.out.println("[One of the guards looks back over to you with a sad expression on his face.]");
      System.out.println("HADVAR: Wait, you there. Step forward. Who are you?");

   }

   }

2 个答案:

答案 0 :(得分:3)

问题的一些修整:

do {

   next = input.next();

while (!next.startsWith("y") || !next.startsWith("n"));

最简单的解释是从布尔代数的角度来看它:

not(x) or not(y) = not(x and y)

当我们用x和y代替您的字符串条件时:

not(next.startsWith("y")) or not(next.startsWith("n")) 

等于

not(next.startsWith("y") and next.startsWith("n"))

由于字符串永远不能以“ n”和“ y”开头,因此该语句始终为假,因此其否定始终为真。这就是为什么它是一个无休止的循环。

解决方案:做到

do {

   next = input.next();

while (!next.startsWith("y") && !next.startsWith("n"));

我想这就是您想要的。

答案 1 :(得分:0)

这两者不能一次都为假

!next.startsWith("y") || !next.startsWith("n")

假设输入为y

!true || !false
false || true
true

然后循环再次重复。

也许是您要寻找的行为。

!next.startsWith("y") && !next.startsWith("n")