Reputation: 19
I am not allowed to use more than three print statements. I tried doing by creating private method but it didn't work out.
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;
}
Upvotes: 0
Views: 1345
Reputation: 11
You can try to store the output text in an instance variable then use one print statement for all your outputs.
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";
}
Upvotes: 1
Reputation: 533442
You appear to want to round up. One way to do this is
int numBills = (int) Math.ceil(amount / 20);
Upvotes: 0
Reputation: 26
Something like this
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;
}
Upvotes: 0
Reputation: 11
As it was already mentioned you can concatenate the two strings and make your print on the methode a System.out.println("Text here");
Also you don't need to make the scanner a argument on the methode, you could simply, before the main methode, type:
static Scanner output = new Scanner (System.in);
This way you can use the scanner throughout the (kind of) whole program like this:
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;
}
Upvotes: 0
Reputation: 59950
You have two solutions, you can use just two if you want you can just concatenate your String using \n
:
public static void main(String[] args) {
//...
System.out.println("John needs " + numBills1 + " bills" + "\n" + "Jane needs " + numBills2 + " bills");
//---------------------------------------------------------^^^----------
}
and in your method spending
you can change print
to println
:
public static int spending(Scanner console, String name) {
System.out.println("How much will " + name + " be spending? ");
//--------------^^---------------------------------------------
and remove the :
System.out.println();
Upvotes: 0
Reputation: 50809
What about using new line \n
System.out.println("John needs " + numBills1 + " bills\nJane needs " + numBills2 + " bills");
You also don't really need the empty print. Remove
System.out.println();
Upvotes: 0