如何循环我的密码验收程序

时间:2015-06-26 17:00:48

标签: java loops

我对java很新(就像2天前开始的那样),我想知道如何循环这个程序。我试着做功能但是它一直说我的变量不能被解析为变量。

这是代码:

class GS1 {

    public static void main(String[]args){
        do {
            System.out.println("Enter your password");
            Scanner F_pass = new Scanner(System.in);
            String f_word= F_pass.next();
            System.out.println("Verify Password");
            Scanner Pass = new Scanner(System.in);
            String word = Pass.next();
        } while(f_word != word);
    }
}

1 个答案:

答案 0 :(得分:0)

这似乎可行。通常,您只需要一个Scanner对象实例,它将在整个类中工作 - 具体取决于您使用的范围。

public static void main(String[] args)
 {
  // TODO Auto-generated method stub
  Scanner input = new Scanner(System.in);
  String origPass = "Original";
  String verify = "Verify";

  while(!origPass.equals(verify)){

     System.out.println("Enter your password");
     origPass = input.nextLine();

     System.out.println("Verify your password");
     verify = input.nextLine();
  }
 }

我所做的只是比较两个字符串的值。如果它们不相同,您的程序将重复,直到满足条件。如果用户没有匹配的值,您可以添加一个print语句,让用户知道密码不正确。希望这是你想要的。