Skip to content

Instantly share code, notes, and snippets.

@ajbinky
Created October 3, 2018 00:22
Show Gist options
  • Save ajbinky/58fa5fb3fbfd89ff1ee70c3e9e822e56 to your computer and use it in GitHub Desktop.
Save ajbinky/58fa5fb3fbfd89ff1ee70c3e9e822e56 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class AverageAgeModified {
public static void main(String[] args) {
// Add scanner so we can get user input
Scanner scan = new Scanner(System.in);
int total = 0;
int counter = 0;
int ages = 0;
System.out.print("Enter number of ages: ");
ages = scan.nextInt();
while (counter < ages) {
System.out.print("Enter age: ");
total += scan.nextInt();
counter++;
}
scan.close();
double average = total / (double) ages;
System.out.println("Average age is " + average);
}
}
import java.util.Scanner;
public class BookOrderForLoop {
private static double subtotal = 0;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of books: ");
int numberofbooks = scan.nextInt();
for (int i = 0; i < numberofbooks; i++) {
System.out.print("Enter book " + (i+1) + "'s cost: ");
subtotal += scan.nextDouble();
}
scan.close();
final double tax = 6.5;
final double shipping_charge = 2.95;
double totalTax = tax * subtotal / 100;
double totalshipping = shipping_charge * numberofbooks;
double ordertotal = subtotal + totalTax + totalshipping;
System.out.printf("Number of books: %d\n", numberofbooks);
System.out.printf("Book subtotal: %.2f\n", subtotal);
System.out.printf("Tax: %.2f\n", totalTax);
System.out.printf("Shipping: %.2f\n", totalshipping);
System.out.printf("Order total: %.2f\n", ordertotal);
}
}
import java.util.Scanner;
public class BookOrderSentinal {
private static double subtotal = 0;
private static int numberofbooks = 0;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double cost = 0;
do {
System.out.print("Enter book " + (numberofbooks+1) + "'s cost (or -1 to quit): ");
cost = scan.nextDouble();
if (cost != -1) {
numberofbooks++;
subtotal += cost;
}
} while (cost != -1);
scan.close();
final double tax = 6.5;
final double shipping_charge = 2.95;
double totalTax = tax * subtotal / 100;
double totalshipping = shipping_charge * numberofbooks;
double ordertotal = subtotal + totalTax + totalshipping;
System.out.printf("Number of books: %d\n", numberofbooks);
System.out.printf("Book subtotal: %.2f\n", subtotal);
System.out.printf("Tax: %.2f\n", totalTax);
System.out.printf("Shipping: %.2f\n", totalshipping);
System.out.printf("Order total: %.2f\n", ordertotal);
}
}
import java.util.Scanner;
public class PrintStarts {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String stars = "***********";
int counter = 0;
System.out.print("Enter number of rows: ");
int x = scan.nextInt();
scan.close();
do {
System.out.println(stars);
counter++;
} while (counter < x);
}
}
import java.util.Scanner;
public class TicketCounter {
private static int ticketsAvailable = 75;
private static int counter = 0;
private static final int MAX_TICKETS_PER_CUSTOMER = 6;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(ticketsAvailable > 0) {
System.out.print("Enter number of tickets: ");
int tickets = scan.nextInt();
if (tickets > 0 && tickets <= MAX_TICKETS_PER_CUSTOMER && ticketsAvailable >= tickets) {
ticketsAvailable -= tickets;
counter++;
} else {
System.out.println("Incorrect input. Try again.");
}
}
scan.close();
System.out.println(counter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment