import javax.swing.JOptionPane;
public class contohwhile {
/** Main method */
public static void main(String[]args){
// Read an initial data
String dataString = JOptionPane.showInputDialog
("Masukan angka integer : \n(Program akan berakhir jika diinput 0)");
int data = Integer.parseInt(dataString);
// keep reading data until the input is 0
int sum = 0;
while (data !=0){
sum += data;
// Read the next data
dataString = JOptionPane.showInputDialog
("Masukan nilai Integer :\n(Program ini akan berakhir jika diinput 0)");
data = Integer.parseInt(dataString);
}
JOptionPane.showMessageDialog(null, "Totalnya adalah " + sum);
}
}
Senin, 19 Agustus 2013
hasil nilai uas,uts,tugas
import java.io.*;
public class nilai {
public static void main (String []args) throws IOException{
double nilaiakhir;
int uts,uas,tugas,quiz;
String nama;
BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
System.out.println ("Nama anda :");
nama = in.readLine();
System.out.print ("nilai uts : ");
uts = Integer.parseInt(in.readLine());
System.out.print ("nilai uas : ");
uas = Integer.parseInt(in.readLine());
System.out.print ("nilai tugas : ");
tugas = Integer.parseInt(in.readLine());
System.out.print ("nilai quiz : ");
quiz = Integer.parseInt(in.readLine());
nilaiakhir = ((0.3*uts)+(0.4*uas)+(0.2*quiz)+(0.1*tugas));
System.out.println("====================================");
System.out.println();
System.out.println();
System.out.println("saudara " +nama+" "+"nilai akhir anda"+" "+nilaiakhir);
if (nilaiakhir <=100 && nilaiakhir >=80){
System.out.println("Nilai anda 'A'");
}
else if (nilaiakhir <= 79 && nilaiakhir >= 75)
{
System.out.println("nilai anda 'B' ");
}
else if(nilaiakhir <=74 && nilaiakhir >= 65)
{
System.out.println("Nilai anda 'C' ");
}
else if (nilaiakhir <= 64 && nilaiakhir >= 50) {
System.out.println (" Nilai anda 'D' ");
}
else
{
System.out.println(" Nilai anda 'E' ");
}
}
}
public class nilai {
public static void main (String []args) throws IOException{
double nilaiakhir;
int uts,uas,tugas,quiz;
String nama;
BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
System.out.println ("Nama anda :");
nama = in.readLine();
System.out.print ("nilai uts : ");
uts = Integer.parseInt(in.readLine());
System.out.print ("nilai uas : ");
uas = Integer.parseInt(in.readLine());
System.out.print ("nilai tugas : ");
tugas = Integer.parseInt(in.readLine());
System.out.print ("nilai quiz : ");
quiz = Integer.parseInt(in.readLine());
nilaiakhir = ((0.3*uts)+(0.4*uas)+(0.2*quiz)+(0.1*tugas));
System.out.println("====================================");
System.out.println();
System.out.println();
System.out.println("saudara " +nama+" "+"nilai akhir anda"+" "+nilaiakhir);
if (nilaiakhir <=100 && nilaiakhir >=80){
System.out.println("Nilai anda 'A'");
}
else if (nilaiakhir <= 79 && nilaiakhir >= 75)
{
System.out.println("nilai anda 'B' ");
}
else if(nilaiakhir <=74 && nilaiakhir >= 65)
{
System.out.println("Nilai anda 'C' ");
}
else if (nilaiakhir <= 64 && nilaiakhir >= 50) {
System.out.println (" Nilai anda 'D' ");
}
else
{
System.out.println(" Nilai anda 'E' ");
}
}
}
contoh pertukaran uang
import javax.swing.JOptionPane;
public class pertukaranuang{
public static void main(String[]args){
String nilaiuang=JOptionPane.showInputDialog("masukan nilai mata uang yang ingin ditukar");
double uang=Double.parseDouble(nilaiuang);
int jumlahuang=(int)(uang);
int nilaiseratusribu=jumlahuang/100000;
jumlahuang=jumlahuang%100000;
int nilailimapuluh=jumlahuang/50000;
jumlahuang=jumlahuang%50000;
int nilaiduapuluh=jumlahuang/20000;
jumlahuang=jumlahuang%20000;
int nilaisepuluh=jumlahuang/10000;
jumlahuang=jumlahuang%10000;
int nilailimaribu=jumlahuang/5000;
jumlahuang=jumlahuang%5000;
int nilaiseribu=jumlahuang/1000;
jumlahuang=jumlahuang%1000;
int nilailimaratus=jumlahuang/500;
jumlahuang=jumlahuang%500;
int nilaiduaratus=jumlahuang/200;
jumlahuang=jumlahuang%200;
int nilaiseratus=jumlahuang/100;
jumlahuang=jumlahuang%100;
String hasil="Uang anda "+nilaiuang+" senilai dengan\n"+
nilaiseratusribu+" lembar seratus ribu\n"+
nilailimapuluh+" lembar lima puluh ribu\n"+
nilaiduapuluh+" lembar dua puluh ribu\n"+
nilaisepuluh+" lemabar sepuluh ribu\n"+
nilailimaribu+" lembar lima ribu\n"+
nilaiseribu+" koin seribu\n"+
nilailimaratus+" koin lima ratus\n"+
nilaiduaratus+" koin dua ratus\n"+
nilaiseratus+" koin seratus";
JOptionPane.showMessageDialog(null,hasil);
}
}
public class pertukaranuang{
public static void main(String[]args){
String nilaiuang=JOptionPane.showInputDialog("masukan nilai mata uang yang ingin ditukar");
double uang=Double.parseDouble(nilaiuang);
int jumlahuang=(int)(uang);
int nilaiseratusribu=jumlahuang/100000;
jumlahuang=jumlahuang%100000;
int nilailimapuluh=jumlahuang/50000;
jumlahuang=jumlahuang%50000;
int nilaiduapuluh=jumlahuang/20000;
jumlahuang=jumlahuang%20000;
int nilaisepuluh=jumlahuang/10000;
jumlahuang=jumlahuang%10000;
int nilailimaribu=jumlahuang/5000;
jumlahuang=jumlahuang%5000;
int nilaiseribu=jumlahuang/1000;
jumlahuang=jumlahuang%1000;
int nilailimaratus=jumlahuang/500;
jumlahuang=jumlahuang%500;
int nilaiduaratus=jumlahuang/200;
jumlahuang=jumlahuang%200;
int nilaiseratus=jumlahuang/100;
jumlahuang=jumlahuang%100;
String hasil="Uang anda "+nilaiuang+" senilai dengan\n"+
nilaiseratusribu+" lembar seratus ribu\n"+
nilailimapuluh+" lembar lima puluh ribu\n"+
nilaiduapuluh+" lembar dua puluh ribu\n"+
nilaisepuluh+" lemabar sepuluh ribu\n"+
nilailimaribu+" lembar lima ribu\n"+
nilaiseribu+" koin seribu\n"+
nilailimaratus+" koin lima ratus\n"+
nilaiduaratus+" koin dua ratus\n"+
nilaiseratus+" koin seratus";
JOptionPane.showMessageDialog(null,hasil);
}
}
tutorial pengulangan loop (J2SE)
import javax.swing.JOptionPane;
public class tutorialpengulanganloop{
public static void main(String[]args){
int option=0;
while(option==JOptionPane.YES_OPTION){
int correctCount = 0;
int count = 0; // Count the number of questions
long startTime = System.currentTimeMillis();
String output = "";
while (count < 10) {
// 1. Generate two random single-digit integers
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);
// 2. If number1 < number2, swap number1 with number2
if (number1 <number2){
int temp = number1;
number1 = number2;
number2 = temp;
}
// 3. Prompt the student to answer "what is number1 number2?"
String answerString = JOptionPane.showInputDialog
("What is " + number1 + " - " + number2 + "?");
int answer = Integer.parseInt(answerString);
// 4. Grade the answer and display the result
String replyString;
if (number1 - number2 == answer){
replyString = "You are correct !";
correctCount++;
}
else
replyString = "You answer is wrong.\n" + number1 + " - "
+ number2 + " should be " + (number1 - number2);
JOptionPane.showMessageDialog(null, replyString);
// Increase the count
count++;
output += "\n" + number1 + "-" + number2 + "=" + answerString +
((number1 - number2 == answer) ? "correct" : "wrong");
}
long endTime = System.currentTimeMillis();
long testTime = endTime - startTime;
JOptionPane.showMessageDialog(null,
"Correct count is " + correctCount + "\nTest time is " +
testTime / 1000 + " seconds\n" + output);
System.out.println("continue loop");
option = JOptionPane.showConfirmDialog(null, "continue?");
}
}
}
public class tutorialpengulanganloop{
public static void main(String[]args){
int option=0;
while(option==JOptionPane.YES_OPTION){
int correctCount = 0;
int count = 0; // Count the number of questions
long startTime = System.currentTimeMillis();
String output = "";
while (count < 10) {
// 1. Generate two random single-digit integers
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);
// 2. If number1 < number2, swap number1 with number2
if (number1 <number2){
int temp = number1;
number1 = number2;
number2 = temp;
}
// 3. Prompt the student to answer "what is number1 number2?"
String answerString = JOptionPane.showInputDialog
("What is " + number1 + " - " + number2 + "?");
int answer = Integer.parseInt(answerString);
// 4. Grade the answer and display the result
String replyString;
if (number1 - number2 == answer){
replyString = "You are correct !";
correctCount++;
}
else
replyString = "You answer is wrong.\n" + number1 + " - "
+ number2 + " should be " + (number1 - number2);
JOptionPane.showMessageDialog(null, replyString);
// Increase the count
count++;
output += "\n" + number1 + "-" + number2 + "=" + answerString +
((number1 - number2 == answer) ? "correct" : "wrong");
}
long endTime = System.currentTimeMillis();
long testTime = endTime - startTime;
JOptionPane.showMessageDialog(null,
"Correct count is " + correctCount + "\nTest time is " +
testTime / 1000 + " seconds\n" + output);
System.out.println("continue loop");
option = JOptionPane.showConfirmDialog(null, "continue?");
}
}
}
Langganan:
Postingan (Atom)