程序在运行并输入正确的输入后不会显示程序的答案

时间:2015-06-01 14:58:23

标签: java joptionpane

所以我有我的程序,它不会在最后一个问题(需要输入)之后运行。我的代码在这里:

import java.util.Scanner;

import javax.swing.JOptionPane;

public class HiConor
{

   public static void main(String[] args)
   {

      String result; 
      int selection;
      int Entry;
      boolean isYes;
      System.out.print("Starting Program");
      System.out.print(".");
      System.out.print("");
      System.out.print(".");
      System.out.print("");
      System.out.print(".");
      System.out.print("");
      System.out.print(".");
      JOptionPane.showMessageDialog(null, "Hello there!");
      selection = JOptionPane.showConfirmDialog(null, "Are you doing good?");
      boolean isNo;
      boolean isCancel;
      isNo = (selection == JOptionPane.NO_OPTION);
      isYes = (selection == JOptionPane.YES_OPTION);
      isCancel = (selection == JOptionPane.CANCEL_OPTION);
      if(isYes){
      JOptionPane.showMessageDialog(null, "Yes?" + " That's nice to hear!");
         } 
      if(isNo){
      JOptionPane.showMessageDialog(null, "No?" + " Well then, cheer up!");
         } 
      if(isCancel){
      JOptionPane.showMessageDialog(null, "Fine then, don't respond");
         }   
      Scanner keyBoard = new Scanner(System.in);
      Scanner input = new Scanner(System.in);
      JOptionPane.showInputDialog(null, "What is your name?",
      "Mlg recess poofs",
      JOptionPane.ERROR_MESSAGE);
      String userinput = input.next();
      if(userinput.equals("alex")){
         JOptionPane.showMessageDialog(null, "What a lovely name!");
         }

      JOptionPane.showMessageDialog(null, "I have a feeling you're trolling me " + ". This is the end of the program. Goodbye.");
      }}

2 个答案:

答案 0 :(得分:1)

将其更改为此

String userinput =  JOptionPane.showInputDialog(null, "What is your name?",
      "Mlg recess poofs",
      JOptionPane.ERROR_MESSAGE);

卸下:

Scanner input = new Scanner(System.in);
String userinput = input.next();

答案 1 :(得分:0)

您正在混合输入法。您是想从对话框中获取用户的响应还是从控制台窗口输入用户的响应?您似乎正在使用JOptionPane,因此我假设您想要对话框。在这种情况下,您根本不需要使用Scanner对象。对JOptionPane.showInputDialog的调用已经返回用户键入的内容,但您不会将其保存在任何变量中。只需执行以下操作:

String userinput = JOptionPane.showInputDialog(null, "What is your name?",
    "Mlg recess poofs",
    JOptionPane.ERROR_MESSAGE);

并且不要打电话给input.next()