Qanday qilib bu kodning ortiqcha miqdorini kamaytira olaman?

Uchdan ortiq nashrdan foydalanishga ruxsat berilmaydi. Men maxsus usulni yaratishga urinib ko'rdim, lekin u ishlamadi.

public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    int numBills1 = spending(console, "John");
    int numBills2 = spending(console, "Jane");
    System.out.println("John needs " + numBills1 + " bills");
    System.out.println("Jane needs " + numBills2 + " bills");
}

public static int spending(Scanner console, String name) {
    System.out.print("How much will " + name + " be spending? ");
    double amount = console.nextDouble();
    System.out.println();
    int numBills = (int) (amount/20.0);
    if (numBills * 20.0 < amount) {
        numBills++;
    }
    return numBills;
}
0
Men ushbu savolni mavzu tashqari yopish uchun ovoz beraman, chunki u codereview.stackexchange.com ga tegishli
qo'shib qo'ydi muallif ꜱᴜʀᴇꜱʜ ᴀᴛᴛᴀ, manba
Xususiy usul qanday ishlamadi?
qo'shib qo'ydi muallif Ole V.V., manba
Stack Overflow ga xush kelibsiz! Iltimos, turga kiring , atrofga nazar tashlang va yordam markazi , ayniqsa Yaxshi savolni qanday berishim mumkin? va Bu yerda qanday mavzular haqida savollar berishim mumkin? . Iltimos, Kodning kutganingizga mos kelmaydigan tavsifini kiriting.
qo'shib qo'ydi muallif Timothy Truckle, manba
Yangi satrdan foydalaning
qo'shib qo'ydi muallif Jack Flamp, manba
Agar ushbu kod ishlayotgan bo'lsa, Kodni tekshirishni so'rashingiz kerak.
qo'shib qo'ydi muallif Joe C, manba

6 javoblar

Chiqish matnini bir misol değişkenine saqlashga harakat qilib, barcha çıktılarınız uchun bir kiritish ifodasini foydalaning.

private static String output="";
public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    spending(console, "John");
    spending(console, "Jane");
    System.out.println(output);
}
public static void spending(Scanner console, String name) {
    System.out.print("How much will " + name + " be spending? ");
    double amount = Double.parseDouble(console.nextLine());
    int numBills = (int) (amount/20.0);
    if (numBills * 20.0 < amount) {
        numBills++;
    }
    output += name + " needs "+numBills+" bills\n";
}
1
qo'shib qo'ydi

Siz ikkita echim ga ega bo'lsangiz, \ n dan foydalanib sizning stringingizni faqat bittadan bitirishingiz mumkin bo'lsa, faqat ikkita </​​strong> dan foydalanishingiz mumkin:

public static void main(String[] args) {
    //...
    System.out.println("John needs " + numBills1 + " bills" + "\n" + "Jane needs " + numBills2 + " bills");
    //---------------------------------------------------------^^^----------
}

sarflash usulida println kiritish ni o'zgartirishingiz mumkin:

public static int spending(Scanner console, String name) {
    System.out.println("How much will " + name + " be spending? ");
    //--------------^^---------------------------------------------

va quyidagilarni olib tashlang:

System.out.println();
0
qo'shib qo'ydi

Yuvarlamoqchi bo'lgan ko'rinasiz. Buning bir usuli bor

int numBills = (int) Math.ceil(amount/20);
0
qo'shib qo'ydi

What about using new line \n

System.out.println("John needs " + numBills1 + " bills\nJane needs " + numBills2 + " bills");

Bundan tashqari siz bo'sh nusxaga muhtoj emassiz. O'chiring

System.out.println();
0
qo'shib qo'ydi

As it was already mentioned you can concatenate the two strings and make your print on the methode a System.out.println("Text here");

Bundan tashqari, skanerni metodga argument qo'yish shart emas, oddiy metoddan oldin shunday bo'lishi mumkin:

static Scanner output = new Scanner (System.in);

Brauzerni ushbu dasturning barcha turlari bo'yicha quyidagi tarzda ishlatishingiz mumkin:

public static int spending(String name) 
{
        System.out.println("How much will " + name + " be spending? ");
        double amount = output.nextDouble();  

        int numBills = (int) (amount/20.0);
        if (numBills * 20.0 < amount)
        {
            numBills++;
        }
        return numBills;
}
0
qo'shib qo'ydi

Shunga o'xshash narsa

  public static void main(String[] args) {
    while (true) {
        Scanner console = new Scanner(System.in);
        System.out.print("The name : ");
        String name = console.nextLine();
        System.out.print("How much will " + name + " be spending? ");
        double amount = console.nextDouble();
        System.out.println(name + " needs " + calculateBills(amount) + " bills");
    }
}

private static int calculateBills(double amount) {
    int numBills = (int) (amount/20.0);
    if (numBills * 20.0 < amount) {
        numBills++;
    }
    return numBills;
}
0
qo'shib qo'ydi