java找出每个变量代表的总量是多少?
我试图找出变量1的总金额和变量2的总成本分别是多少。到目前为止,我已经附上了我的源代码的图片
作业说明是:然后程序将计算所有费用的总和,如果费用平分,每个人应该支付什么,以及每个朋友实际支付的金额。如果一个人支付的钱比另一个人少,那么他们将欠朋友一些钱
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);
} }
# 1 楼答案
假设你有两个人,分别是麦克斯和西蒙。你可以做下面这样的事情,首先使用你的逻辑找到Max所欠的金额。然后用
totalCost - amountMaxOwes
查找西蒙所欠的金额:用法示例: