Tình hình là vừa bắt đầu học code Java, chưa biết gì ông thầy đã ném vào mặt 3 bài bảo về nhà làm :'( Mà mò mãi cũng ko ra, ai giúp dùm tiểu đệ :'( Mã: public static void main ( String[] name) { System.out.print (" Gershwin, George"); } Mã: public class WelcomeTimes5 { public static void main ( String args [ ] ) { System.out.print ("Welcome to Java\n"); System.out.printf ("Welcome to Java\n"); System.out.printf ("Welcome to Java\n"); System.out.printf ("Welcome to Java\n"); System.out.printf ("Welcome to Java\n"); } }
Suppose your name was George Gershwin. Write a complete main method that would print your last name , followed by a comma, followed by a space and your first name . Mã: public static void main(String[] args) { System.out.print (" Gershwin, George"); } Write a program that displays "Welcome to Java" five times. Mã: public static void main(String[] args) { //Nên dùng for System.out.println("Welcome to Java"); System.out.println("Welcome to Java"); System.out.println("Welcome to Java"); System.out.println("Welcome to Java"); System.out.println("Welcome to Java"); } Write a program that when you input minutes, it comes out as years and days Mã: public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter minutes: "); int minutes = input.nextInt(); int totalDays = minutes / (24 * 60); int years = totalDays / 365; int days = totalDays % 365; System.out.println("Days: "+days); System.out.println("Years: "+years); }
Thank you thank you Giúp thêm dùm mình câu này :( Mùa này xui quá, dzô trúng ông thầy lãng xẹt, vào lớp chỉ giải thích công dụng rồi thôi, chã chỉ cách dùng code thế nào, rồi ném bài cho làm :(
Mình viết thế này, chạy thì chạy tốt, mà ko biết đúng theo đề yêu cầu không, vì khi chạy thì nó hỏi 4 công thức cùng lúc Mà ông thầy muốn kiểu chọn hình rồi mới nhập số liệu :( Mã: //Khang Le import java.util.Scanner; public class InputAreaVolume { public static void main(String[] args) { // Cube System.out.print("Enter edge measurement of cube: "); double edge = extracted().nextDouble(); double CubeArena = 6 * edge * edge; double CubeVolume = edge * edge * edge; System.out.println(" The area of the cube is: " + CubeArena); System.out.println(" The volume of the cube is: " + CubeVolume); // Sphere System.out.print("Enter radius measurement of sphere: "); double radius = extracted().nextDouble(); double SphereArena = 4 * 3.14 * radius * radius; double SphereVolume = Math.round(3.14 * radius * radius * radius * 4/3) ; System.out.println(" The area of the sphere is: " + SphereArena); System.out.println(" The volume of the sphere is: " + SphereVolume); // Cylinder System.out.print("Enter radius measurement of cylinder: "); double cylinder = extracted().nextDouble(); System.out.print("Enter height measurement of cylinder: "); double height = extracted().nextDouble(); double CylinderArena = 2 * 3.14 * cylinder * height + 2 * 3.14 * cylinder * cylinder; double CylinderVolume = 3.14 * cylinder * cylinder * height; System.out.println(" The area of the cylinder is: " + CylinderArena); System.out.println(" The volume of the cylinder is: " + CylinderVolume); // Tetrahedron System.out.print("Enter edge measurement of tetrahedron: "); double tetrahedron = extracted().nextDouble(); double TetrahedronArena = Math.round( Math.sqrt(3) * tetrahedron * tetrahedron); double TetrahedronVolume =Math.round((tetrahedron * tetrahedron * tetrahedron) / (6 * Math.sqrt(2))); System.out.println(" The area of the tetrahedron is: " + TetrahedronArena); System.out.println(" The volume of the tetrahedron is: " + TetrahedronVolume); } private static Scanner extracted() { return new Scanner(System.in); } }
Vì không rõ yêu cầu nên mình làm đại nhé ! Bác muốn thế nào cứ cụ thể hơn mình chỉnh lại. Mã: System.out.print("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Tetrahedron"); double number = extracted().nextInt(); // Nên dùng switch if(number == 1) { //Copy cube's code } else if(number == 2) { //Copy sphere's code } ............................. else { System.out.println("Invalid input"); System.exit(1); } Cho mình hỏi ngoài lê chút bác đang học Java ở đâu thế, mình cũng mới tự học Java
Điểm hẹn lại đến, cũng vẫn là cái project lần trước, mà lần này ông thầy bảo muốn dùng switch-case với method. Method là sao? Giải thích hộ mình :( Mã: import java.util.Scanner; public class FindArea { public static void main(String[] args) { Scanner input = new Scanner(System.in); // TODO Auto-generated method stub System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut"); int n = input.nextInt(); if(n == 1) { System.out.print("Enter side measurement of cube: "); double side = input.nextDouble(); double CubeArea = 6 * side * side; System.out.println("The area of the cube is: " + CubeArea); } if (n == 2){ System.out.print("Enter radius measurement of sphere: "); double radius = input.nextDouble(); double SphereArea = 4 * 3.14 * radius * radius; System.out.println("The area of the sphere is: " + SphereArea); } if (n == 3){ System.out.print("Enter radius measurement of cylinder: "); double cylinder = input.nextDouble(); System.out.print("Enter height measurement of cylinder: "); double height = input.nextDouble(); double CylinderArea = 2 * 3.14 * cylinder * height + 2 * 3.14 * cylinder * cylinder; System.out.println("The area of the cylinder is: " + CylinderArea); } if (n == 4){ System.out.print("Enter inner radius: "); double inner = input.nextDouble(); System.out.print("Enter outer radius: "); double outer = input.nextDouble(); double DonutArea = 3.14 * (outer*outer - inner*inner); System.out.println("The area of the donut is: " + DonutArea); } } }
Updated. đã làm ra, nhưng cái vòng loop bị lỗi, thay vì exit thì nó lại print thêm 1 lần nữa, fix sao nhỉ :( Mã: import java.util.Scanner; public class FindArea { // side is only input provided to cube. Area of the cube is returned public static double cube (double side) { double Area; return Area = 6 * side * side; } // radius is only input provided to sphere. Area of the sphere is returned public static double sphere (double radius) { double Area; return Area = 4 * 3.14 * radius * radius; } // radius and height are the only inputs provided to cylinder. // Area of the cylinder is returned public static double cylinder (double radius, double height) { double Area; return Area = 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius; } // outerR and innerR are the only inputs provided to doughnut. // Area of the doughnut is returned public static double doughnut (double outerR, double innerR) { double Area; return Area = (2 * 3.14 * innerR) * (2 * 3.14 * outerR); } public static void main(String[] args) { int n = 6; Scanner input = new Scanner(System.in); System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut"); do { n = input.nextInt(); if ( n<0 || n>4) { System.out.println("Invalid"); } if(n == 1) { System.out.print("Enter side measurement of cube: "); double side = input.nextDouble(); double Area = cube(side); System.out.println("The area of the cube is: " + Area); } if (n == 2) { System.out.print("Enter radius measurement of sphere: "); double radius = input.nextDouble(); double Area = sphere (radius); System.out.println("The area of the sphere is: " + Area); } if (n == 3) { System.out.print("Enter radius measurement of cylinder: "); double radius = input.nextDouble(); System.out.print("Enter height measurement of cylinder: "); double height = input.nextDouble(); double Area = cylinder (radius, height); System.out.println("The area of the cylinder is: " + Area); } if (n == 4) { System.out.print("Enter inner radius: "); double innerR = input.nextDouble(); System.out.print("Enter outer radius: "); double outerR = input.nextDouble(); double Area = doughnut (outerR, innerR); System.out.println("The area of the donut is: " + Area); } System.out.println("--------"); System.out.println("Enter 1 for Cube, 2 for Sphere, 3 for Cylinder, 4 for Doughnut"); System.out.println("Or enter 0 to exit"); } while (n != 0); System.exit(1); } }
mi đừng xài do..while. 1 là for 2 là while thôi Mã: import java.util.Scanner; public class FindArea { // side is only input provided to cube. Area of the cube is returned public static double cube (double side) { return 6 * side * side; } // radius is only input provided to sphere. Area of the sphere is returned public static double sphere (double radius) { return 4 * 3.14 * radius * radius; } // radius and height are the only inputs provided to cylinder. // Area of the cylinder is returned public static double cylinder (double radius, double height) { return 2 * 3.14 * radius * height + 2 * 3.14 * radius * radius; } // outerR and innerR are the only inputs provided to doughnut. // Area of the doughnut is returned public static double doughnut (double outerR, double innerR) { return (2 * 3.14 * innerR) * (2 * 3.14 * outerR); } public static void main(String[] args) { Scanner input = new Scanner(System.in); int n; double area, side, radius, height, innerR, outerR; // main loop boolean quit = false; while (!quit) { // get user's choice System.out.println("Enter 1 for Cube\n" + "Enter 2 for Sphere\n" + "Enter 3 for Cylinder\n" + "Enter 4 for Doughnut\n" + "Enter 0 to exit"); n = input.nextInt(); // handle user's choice switch (n) { case 0: quit = true; break; case 1: System.out.print("Enter side measurement of cube: "); side = input.nextDouble(); area = cube(side); System.out.println("The area of the cube is: " + area); break; case 2: System.out.print("Enter radius measurement of sphere: "); radius = input.nextDouble(); area = sphere(radius); System.out.println("The area of the sphere is: " + area); break; case 3: System.out.print("Enter radius measurement of cylinder: "); radius = input.nextDouble(); System.out.print("Enter height measurement of cylinder: "); height = input.nextDouble(); area = cylinder(radius, height); System.out.println("The area of the cylinder is: " + area); break; case 4: System.out.print("Enter inner radius: "); innerR = input.nextDouble(); System.out.print("Enter outer radius: "); outerR = input.nextDouble(); area = doughnut(outerR, innerR); System.out.println("The area of the donut is: " + area); break; default: System.out.println("Invalid"); break; } System.out.println("--------"); } } }
Xem hộ mình cái này :( Mã: public class CreateBankAccounts { public static void main(String[] args) { // create 3 bank account objects initial balances: 100, 5000, 300. BankAccount savings = new BankAccount(); // balance of 100 // Bank account object 1: deposit 100, withdraw 50, output balance // Bank account object 2: withdraw 2000, withdraw 100, output balance // Bank account object 3: deposit 300, deposit 200, withdraw 750, output balance // Cannot access private data members in different class // saving.balance = 0; fails } } Với cả cái này, Mã: public class BankAccount { private int acctNumber = 1000; private double balance = 100; //Defaut Constructor public BankAccount () { balance = 0; } //Initialize the balance public BankAccount (double amount){ balance = amount; } //method to return the balance public double balance (){ return balance; } //method to deposit public void deposit (double money){ double newbalance = balance + money; balance = newbalance; } //method to wirthdraw public void withdraw (double money){ double newbalance = balance - money; balance = newbalance; } //method to immediately add interest to balance given the rate(%) specified in the parameter public void interest (double interest){ } } Không có đề cụ thể, ông thầy chỉ ném cho cái file java với cái comments // rồi kêu dùng UML :(
UML nghe quen quen; còn bài này thì nó cho mình BankAccount sẳn rồi, chỉ việc viết lại hàm main của lớp CreateBankAccounts. ex: Mã: public static void main(String[] args) { BankAccount obj1 = new BankAccount(100); BankAccount obj2 = new BankAccount(5000); BankAccount obj3 = new BankAccount(300); System.out.println("After withdawal:"); //account object 1 obj1.deposit(100); obj1.withdraw(50); System.out.println("Account object 1: " + obj1.balance()); //account object 2 obj2.withdraw(2000); obj2.withdraw(100); System.out.println("Account object 2: " + obj2.balance()); //account object 3 obj3.deposit(300); obj3.deposit(200); obj3.withdraw(750); System.out.println("Account object 3: " + obj3.balance()); } good luck bro