我正在嘗試將 Eclipse 中的 java 檔案匯出到桌面上的可運行 .jar 檔案中。
該程式是銀行的模擬程式,當您啟動時它會提示您一個選單,如下所示:
歡迎約翰·多伊。你的ID是1。
你想干什么?
A. 查看余額 B. 存款 C. 提款 D. 查看之前的交易 E. 計算利息 F. 退出
輸入一個選項:
這是我的主要課程:
public class FirstBank {
public static void main(String[] args) {
Account zach = new Account("John Doe", 000001);
zach.showMenu();
}
}
這是一個包含我所有方法的單獨類:
import java.util.Scanner;
public class Account {
int balance;
int previousTransaction;
String customerName;
int customerID;
//constructor
public Account(String customerName, int customerID) {
this.customerName = customerName;
this.customerID = customerID;
}
public void deposit(int amount) {
if(amount != 0) {
balance = balance amount;
previousTransaction = amount;
}
}
public void withdraw(int amount2) {
if(amount2 <= balance) {
balance = balance - amount2;
previousTransaction = -amount2;
} else {
System.out.println("Error: insufficient funds.");
}
}
public void getPreviousTransaction() {
if (previousTransaction > 0) {
System.out.println("Deposited: " previousTransaction);
} else if (previousTransaction < 0) {
System.out.println("Deposited: " previousTransaction);
} else {
System.out.println("No transaction occurred");
}
}
public void calculateInterest (int years) {
double interestRate = .0185;
double newBalance = (balance * interestRate * years) balance;
System.out.println("The current interest rate is " (100 * interestRate) "%.");
System.out.println("After " years " years, your balance will be $" newBalance ".");
}
public void showMenu () {
char option = '\0';
// creates a scanner object:
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome " customerName ".");
System.out.println("Your ID is " customerID ".");
System.out.println();
System.out.println("What would you like to do?");
System.out.println();
System.out.println("A. Check your balance");
System.out.println("B. Make a deposit");
System.out.println("C. Make a withdrawal");
System.out.println("D. View previous transaction");
System.out.println("E. Calculate interest");
System.out.println("F. Exit");
do {
System.out.println();
System.out.println("Enter an option: ");
char option1 = scanner.next().charAt(0);
option = Character.toUpperCase(option1);
System.out.println();
switch (option) {
case 'A':
System.out.println();
System.out.println("Balance = $" balance);
System.out.println();
break;
case 'B':
System.out.println();
System.out.println("Enter a deposit amount: ");
int amount = scanner.nextInt();
deposit(amount);
System.out.println();
break;
case 'C':
System.out.println();
System.out.println("Enter an amount to withdraw: ");
int amount2 = scanner.nextInt();
withdraw(amount2);
System.out.println();
break;
case 'D':
System.out.println();
getPreviousTransaction();
break;
case 'E':
System.out.println();
System.out.println("Enter the number of years: ");
int years = scanner.nextInt();
calculateInterest(years);
System.out.println();
break;
case 'F':
System.out.println();
System.out.println();
System.out.println();
break;
default:
System.out.println("Invalid input: Please enter A, B, C, D, E, or F.");
break;
}
} while (option != 'F');
System.out.println("Thank you for banking with us!");
}
}
我嘗試將 .jar 檔案和可運行的 .jar 檔案匯出到我的桌面上,但是當我單擊 jar 檔案時,它什么也沒做。
我認為問題可能是檔案提示用戶輸入資訊。
理想情況下,我只想單擊桌面上的 .jar 檔案,輸入我的資訊,然后運行程式(就像在 eclipse 中一樣)。
該代碼在 Eclipse 中完美運行,沒有任何問題,并且我之前從 Eclipse 中匯出了其他代碼作為可運行的 jars 檔案,它們運行完美。除了這些檔案沒有提示用戶輸入。
謝謝!!!!
uj5u.com熱心網友回復:
要檢查 Eclipse 是否正確創建了 jar 檔案,請打開終端視窗并運行
java -jar <yourjarfile>
如果按需要執行,則不再在 Eclipse 上搜索問題。如果沒有,請檢查諸如如何使用 Maven 創建具有依賴項的可執行 JAR 之類的問題?
現在你最好檢查一下為什么 Windows 會執行 jar 檔案。基本上它必須呼叫上述命令并在控制臺中顯示輸出。但我懷疑它會運行
javaw -jar <yourjarfile>
它不會打開終端,因此不會顯示任何輸出。在這種情況下,您需要修復 Windows Explorer 或擴展系結中的問題。看看 https://www.winhelponline.com/blog/repair-jar-file-association-windows/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/514750.html
標籤:爪哇蚀罐出口
