-4
我想知道變量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);
}}
如果麗莎+巴特=總,然後巴特=總 - 麗莎。這是簡單的代數。你應該能夠應用這個來解決你的問題。 –
你好,我在開始時分配了Lisa = 1和Bart = 2,所以當我做Bart = total - Lisa時,它只是從總數中減去1。我想我的教授希望我使用if else語句,因爲其餘的問題都涉及到這個問題。你知道我將如何使用if else語句來處理這個問題嗎? – mattiewattie1