资源描述
JAVA作业:(1008060114 荣欣 信息与计算科学1001班)
PP2.3 编写一个应用程序,读取两个浮点数,然后打印输出它们的和、差、乘积。
import javax.swing.*;
import java.util.*;
public class First {
/**
* 作业PP2.3 编写一个应用程序,读取两个浮点数,然后打印输出它们的和、差、乘积。
*/
public static void main(String[] args) {
float f1=0.0f, f2=0.0f;
float sum=0.0f;
float difference=0.0f;
float product=0.0f;
String tempstr=JOptionPane.showInputDialog("Input Please");
f1=Float.parseFloat(tempstr);
tempstr=JOptionPane.showInputDialog("Input Please Again");
f2=Float.parseFloat(tempstr);
sum=f1+f2;
difference=f1-f2;
product=f1*f2;
JOptionPane.showMessageDialog(null,"The First Number is:"+f1+"\n"+"The Second Number is:"+f2+"\n"+"The Sum is:"+sum+"\n"+"The Difference is:"+difference+"\n"+"The product is:"+product,"Result",JOptionPane.INFORMATION_MESSAGE);
}
}
PP2.4 编写一个应用程序TempConverter1,读取用户输入的华氏温度,然后转换成摄氏温度。
import javax.swing.*;
public class TempConverter1 {
/**
* 作业 PP2.3编写一个应用程序TempConverter1,读取用户输入的华氏温度,然后转换成摄氏温度。
*/
public static void main(String[] args) {
final int BASE=32;
final double CONERSION_FACTOR=5.0/9.0;
float fahrenheiTemp;
double celsiusTemp;
String tempstr=JOptionPane.showInputDialog("Input Please");
fahrenheiTemp=Float.parseFloat(tempstr);
celsiusTemp=(fahrenheiTemp-BASE)*CONERSION_FACTOR;
JOptionPane.showMessageDialog(null,"FahrenheiTemp is:"+fahrenheiTemp+"\n"+"CelsiusTemp is:"+celsiusTemp,"Result",JOptionPane.INFORMATION_MESSAGE);
}
}
PP2.5 编写一个应用程序,将英里转换为千米(1英里等于1.60935千米)。以浮点数类型读取用户输入的英里数。
import javax.swing.JOptionPane;
public class Third {
/**
* 作业PP2.5 编写一个应用程序,将英里转换为千米(1英里等于1.60935千米)。以浮点数类型读取用户输入的英里数。
*/
public static void main(String[] args) {
final double rewire=1.60935;
float mile=0.0f;
double kilometer=0.0f;
String tempstr=JOptionPane.showInputDialog("Input Please");
mile=Float.parseFloat(tempstr);
kilometer=mile*rewire;
JOptionPane.showMessageDialog(null,"Mile is:"+mile+"\n"+"Kilometer:"+kilometer,"Result",JOptionPane.INFORMATION_MESSAGE);
}
}
PP2.6 编写一个应用程序,以小时、分、秒读取时间长度,然后全部换算成秒并打印输出结果(例如,1小时28分42秒等于5322秒)。
import javax.swing.*;
public class forth {
/**
* 作业PP2.6 编写一个应用程序,以小时、分、秒读取时间长度,然后全部换算成秒并打印输出结果(例如,1小时28分42秒等于5322秒)。
*/
public static void main(String[] args) {
int hour,minute,second,change;
final int CONERSION_FACTOR=60;
String tempstr=JOptionPane.showInputDialog("Input Hour Please");
hour=(int)Float.parseFloat(tempstr);
tempstr=JOptionPane.showInputDialog("Input Minute Please");
minute=(int)Float.parseFloat(tempstr);
tempstr=JOptionPane.showInputDialog("Input Second Please");
second=(int)Float.parseFloat(tempstr);
change=hour*CONERSION_FACTOR*CONERSION_FACTOR+minute*CONERSION_FACTOR+second;
JOptionPane.showMessageDialog(null,"The Hour is:"+hour+"\n"+"The Minute is:"+minute+"\n"+"The Second is:"+second+"\n"+"The Change is:"+change,"Result",JOptionPane.INFORMATION_MESSAGE);
}
}
PP2.7 编写一个应用程序,读入一个以秒为单位的时间长度,然后换算成小时、分、秒的组合表达方式并打印输出结果(例如,9999秒等于2小时46分39秒)。
import javax.swing.*;
public class firth {
/**
* 作业 PP2.7 编写一个应用程序,读入一个以秒为单位的时间长度,然后换算成小时、分、秒的组合表达方式并打印输出结果(例如,9999秒等于2小时46分39秒)。
*/
public static void main(String[] args) {
int hour,minute,second,time;
final int CONERSION_FACTOR=60;
String tempstr=JOptionPane.showInputDialog("Input Time Please");
time=(int)Float.parseFloat(tempstr);
hour=(int)time/CONERSION_FACTOR/CONERSION_FACTOR;
minute=(int)(time-hour*CONERSION_FACTOR*CONERSION_FACTOR)/CONERSION_FACTOR;
second=(int)time-hour*CONERSION_FACTOR*CONERSION_FACTOR-minute*CONERSION_FACTOR;
JOptionPane.showMessageDialog(null,"The Time is:"+time+"\n"+"The Hour is:"+hour+"\n"+"The Minute is:"+minute+"\n"+"The Second is:"+second,"Result",JOptionPane.INFORMATION_MESSAGE);
}
}
PP2.9 编写一个应用程序,提示输入一个代表总钱数的双精度值,然后确定每种纸币和硬币需要的最小数量以达到输入的总钱数(假设10人民币为所需要的最大面额)。例如,如果输入的值为47.63(47元6角3分),那么程序应当输出如下结果:
4 ten yuan
1 five yuan
2 one yuan
6 one jiao
3 one fen
import javax.swing.*;
public class sixth {
/**
* 作业 PP2.9 编写一个应用程序,提示输入一个代表总钱数的双精度值,然后确定每种纸币和硬币需要的最小数量以达到输入的总钱数(假设10人民币为所需要的最大面额)。
*/
public static void main(String[] args) {
double money;
int a,b,c,d,e;
final int number=10;
final int number1=5;
String tempstr=JOptionPane.showInputDialog("Input Money Please");
money=(double)Float.parseFloat(tempstr);
a=(int) (money/number);
b=(int)((money-a*number)/number1);
c=(int)((money-a*number-b*number1)/1);
d=(int)((money-a*number-b*number1-c*1)*number)/1;
e=(int)(((money-a*number-b*number1-c*1)*number*number)%10);
JOptionPane.showMessageDialog(null,"The Money is"+" "+money+"\n"+a+" "+"ten yuan"+"\n"+b+" "+"five yuan"+"\n"+c+" "+"one yuan"+"\n"+d+" "+"one jiao"+"\n"+e+" "+"one fen","Result",JOptionPane.INFORMATION_MESSAGE);
}
}
PP2.11 编写一个应用程序,提示输入两个整数分别作为分数的分子和分母,然后打印输出其小数表示。
import javax.swing.*;
public class sevens {
/**
* 作业PP2.11 编写一个应用程序,提示输入两个整数分别作为分数的分子和分母,然后打印输出其小数表示。
*/
public static void main(String[] args) {
int a,b;
float c=0.0000f;
String tempstr=JOptionPane.showInputDialog("Please Input First Integer ");
a=(int)Float.parseFloat(tempstr);
tempstr=JOptionPane.showInputDialog("Please Input Second Integer ");
b=(int)Float.parseFloat(tempstr);
c=(float)a/b;
JOptionPane.showMessageDialog(null,"The First Integer is:"+a+"\n"+"The Second Integer is:"+b+"\n"+"The fraction is:"+c,"Result",JOptionPane.INFORMATION_MESSAGE);
}
}
学习体会:
本学期刚刚接触JAVA,以前只在手机领域中经常听到JAVA软件。在做完这7道题后,我感觉学习JAVA与学习C/C++有不少相似点,都需要循环渐进,有始有终,按部就班,脚踏实地的学习。JAVA是一门有着阶梯性的一门语言,如果要学习它,我觉得最好还是按照JAVA的学习体系,先学习什么,再学习什么,只有这样,我们在学习中才会遇到更少的麻烦。信心,恒心,毅力在学习JAVA是最重要的。要是学习这门语言开始的时候很有兴趣,遇到困难就退缩,这样最终会放弃学习JAVA。编程有的时候就是那么神奇。刚刚开始的时候会遇到很多的困惑,但是一旦把问题解决了,看到自己期望的结果,自己就会感到很兴奋,我想编程的快乐就在于此了。
计算机科学与技术学院
信息与计算科学1001班
1008060114
荣欣
展开阅读全文