2017-10-21 7 views
-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); 

}}

+0

リサ+バートの場合=合計、その後、バート=合計 - リサ。これは単純な代数です。これを適用して問題を解決できるはずです。 –

+0

こんにちは、最初にLisa = 1とBart = 2を割り当てました.Bart = total - Lisaを実行すると、合計から1つが減算されます。他の質問がそれに関係しているので、私の教授は私にif elseステートメントを使うことを望んでいると思います。 if else文を使って私がこれにどのようにアプローチするのか知っていますか? – mattiewattie1

答えて

0

あなたは2人マックスサイモンを持っていると言います。あなたはあなたのロジックを使用しての最大額を最初に見つけたところで次のようなことをすることができます。そして、あなただけ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 
関連する問題