Java 找出每个变量代表的总量的多少?
我试图找出总金额变量1负责多少,总成本变量2负责多少。到目前为止,我已经附上了我的源代码的图片 作业说明是:然后程序将计算所有费用的总和,如果费用平分,每个人应该支付的费用,以及每个朋友实际支付的费用。如果一个人付的钱比另一个人少,那么他们就欠朋友一些钱Java 找出每个变量代表的总量的多少?,java,Java,我试图找出总金额变量1负责多少,总成本变量2负责多少。到目前为止,我已经附上了我的源代码的图片 作业说明是:然后程序将计算所有费用的总和,如果费用平分,每个人应该支付的费用,以及每个朋友实际支付的费用。如果一个人付的钱比另一个人少,那么他们就欠朋友一些钱 import java.util.Scanner; public class Trip { public static void main(String[] args) { Scanner keyboard = new Scanner(S
import java.util.Scanner;
public class Trip {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double flight, hotel, meals, tour, total, owes;
String name1, name2;
double Lisa, Bart;
double input1, input2, input3, input4;
Lisa = 1;
Bart = 2;
System.out.print("Enter the first name: ");
name1 = keyboard.nextLine();
System.out.print("Enter the second name: ");
name2 = keyboard.nextLine();
System.out.print("Enter cost of flights: ");
flight = keyboard.nextDouble();
System.out.print("Who paid for the flights: Enter 1 for Lisa or 2 for Bart ");
input1 = keyboard.nextInt();
System.out.print("Enter cost of hotel: ");
hotel = keyboard.nextDouble();
System.out.print("Who paid for the hotel: Enter 1 for Lisa or 2 for Bart ");
input2 = keyboard.nextInt();
System.out.print("Enter cost of tour: ");
tour = keyboard.nextDouble();
System.out.print("Who paid for the tour: Enter 1 for Lisa or 2 for Bart ");
input3 = keyboard.nextInt();
System.out.print("Enter cost of meals: ");
meals = keyboard.nextDouble();
System.out.print("Who paid for the meals: Enter 1 for Lisa or 2 for Bart ");
input4 = keyboard.nextInt();
total = flight + hotel + meals + tour;
System.out.printf("Total bill for trip: %.2f \n", total);
owes = total / 2;
System.out.printf("Each person owes: %.2f", owes);
}
}假设你有两个人Max和Simon。您可以执行以下操作,首先使用您的逻辑查找Max所欠的金额。然后,要查找Simon欠下的金额,只需使用totalCost-amountMaxOwes:
import java.util.Scanner;
class Trip {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first name:");
String nameOne = scanner.nextLine();
System.out.print("Enter the second name:");
String nameTwo = scanner.nextLine();
System.out.print("Enter cost of flights:");
double flightsCost = scanner.nextDouble();
System.out.printf("Who paid for the flights? Enter 1 for %s or 2 for %s:", nameOne, nameTwo);
int whoPaidFlights = scanner.nextInt();
System.out.print("Enter cost of hotel:");
double hotelCost = scanner.nextDouble();
System.out.printf("Who paid for the hotel? Enter 1 for %s or 2 for %s:", nameOne, nameTwo);
int whoPaidHotel = scanner.nextInt();
System.out.print("Enter cost of tour:");
double tourCost = scanner.nextDouble();
System.out.printf("Who paid for the tour? Enter 1 for %s or 2 for %s:", nameOne, nameTwo);
int whoPaidTour = scanner.nextInt();
System.out.print("Enter cost of meals:");
double mealsCost = scanner.nextDouble();
System.out.printf("Who paid for the meals? Enter 1 for %s or 2 for %s:", nameOne, nameTwo);
int whoPaidMeals = scanner.nextInt();
double totalCost = flightsCost + hotelCost + tourCost + mealsCost;
System.out.printf("Total bill for trip: %.2f \n", totalCost);
// This stuff can be moved to a more appropriate place if you want to
double personOnePaid = 0;
if(whoPaidFlights == 1) personOnePaid += flightsCost;
if(whoPaidHotel == 1) personOnePaid += hotelCost;
if(whoPaidTour == 1) personOnePaid += tourCost;
if(whoPaidMeals == 1) personOnePaid += mealsCost;
double personTwoPaid = totalCost - personOnePaid;
System.out.printf("%s owes: %.2f \n", nameOne, personOnePaid);
System.out.printf("%s owes: %.2f \n", nameTwo, personTwoPaid);
}
}
用法示例:
Enter the first name: Max
Enter the second name: Simon
Enter cost of flights: 299.34
Who paid for the flights? Enter 1 for Max or 2 for Simon: 1
Enter cost of hotel: 300.40
Who paid for the hotel? Enter 1 for Max or 2 for Simon: 2
Enter cost of tour: 55.00
Who paid for the tour? Enter 1 for Max or 2 for Simon: 1
Enter cost of meals: 314.15
Who paid for the meals? Enter 1 for Max or 2 for Simon: 1
Total bill for trip: 968.89
Max owes: 668.49
Simon owes: 300.40
如果Lisa+Bart=total,则Bart=total-Lisa。这是简单代数。你应该能够应用这个来解决你的问题。你好,我在开始时分配了Lisa=1和Bart=2,所以当我做Bart=total-Lisa时,它只是从总数中减去一。我想我的教授希望我使用if-else语句,因为其余的问题都涉及到这一点。你知道我将如何使用if-else语句来处理这个问题吗?