因此,我創建的程序允許用戶登錄到「工資申請」。用戶名,密碼和帳號已被硬編碼,用戶可以三次嘗試登錄。如果三次嘗試失敗,則程序將終止。如果用戶成功登錄,程序將繼續運行並打開一個菜單。我遇到的問題是,一旦用戶成功登錄,程序將不會繼續運行(不會打開菜單)。我可能錯過了一些非常基本的東西,但我似乎無法找到在哪裏我出錯了。任何更正都表示讚賞。用戶'登錄'後程序將不會繼續運行
菜單不完整,我只是試圖使它在此刻正確運行。
感謝
我的代碼;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Assignment4Test {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
final int Username = 3387;
final int Password = 5183;
final int AccountNumber = 22334455;
int EnteredUsername;
int EnteredPassword;
int EnteredAccountNumber;
for(int s=0;s<=3;s++)
{if (s<3)
{System.out.println("Enter Username");
EnteredUsername = console.nextInt();
System.out.println("Username Entered is " + EnteredUsername);
System.out.println("Enter Password");
EnteredPassword = console.nextInt();
System.out.println("Password Entered is " + EnteredPassword);
System.out.println("Enter Account Number");
EnteredAccountNumber = console.nextInt();
System.out.println("Account Number Entered is " + EnteredAccountNumber);
if (Username == EnteredUsername && (Password == EnteredPassword)
&& (AccountNumber == EnteredAccountNumber)) {
System.out.println("Welcome");
break;
}
else {
System.out.println("Wrong Username, Password or Account Number. Please try again.");
}
}
else
{
System.out.println("3 incorrect enteries detected. Program is terminating, goodbye!");
}
}
class Menu extends JFrame {
JMenuBar menubar;
JMenu TransferAnAmount;
JMenuItem TransferAnAmountToAnotherAccount;
JMenu ListRecentTransactions;
JMenuItem ShowList;
JMenu DisplayCurrentBalance;
JMenuItem ShowBalance;
JMenu ExitProgram;
JMenuItem Exit;
public Menu() {
setLayout (new FlowLayout());
menubar = new JMenuBar();
setJMenuBar (menubar);
TransferAnAmount = new JMenu("Transfer An Amount");
menubar.add(TransferAnAmount);
ListRecentTransactions = new JMenu("List Recent Transactions");
menubar.add(ListRecentTransactions);
DisplayCurrentBalance = new JMenu("Display Current Balance");
menubar.add(DisplayCurrentBalance);
ExitProgram = new JMenu("Exit Program");
menubar.add(ExitProgram);
TransferAnAmountToAnotherAccount = new JMenuItem("Transer an amount to another account");
TransferAnAmount.add(TransferAnAmountToAnotherAccount);
ShowList = new JMenuItem("Show List");
ListRecentTransactions.add(ShowList);
ShowBalance = new JMenuItem("Show Balance");
DisplayCurrentBalance.add(ShowBalance);
Exit = new JMenuItem("Exit Program");
ExitProgram.add(Exit);
event e = new event();
Exit.addActionListener(e);
}
class event implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public void main (String args[]) {
Menu gui = new Menu();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(600,300);
gui.setVisible(true);
}
}
}
}
你好,這偉大的工作,但原來你而提供的代碼繼續打開菜單三次連後,不正確的嘗試經過三次不正確的登錄嘗試的程序終止。有沒有辦法讓程序保持在三次不正確的登錄嘗試後終止,並且只有在成功登錄後才能打開菜單。除此之外,它可以正常工作,謝謝。 – Yaz
好的,修好了!請點擊綠色箭頭「接受」答案,如果你現在對它感到滿意:) – vikingsteve
真棒謝謝你:) – Yaz