本文目录一览:
写几个简单的Java程序,计算学费之类的
你好!很高兴为你解答。
首先,为你提供两个类,一个是StuFee
对象,用于模拟学费对象。
二是TestStuFee
对象,用于运行测试学费对象,解决问题。
其次,以下是运行结果:
**********************************
run:
第十年学费:15513.282159785162
第11到第14年70207.39453239123
请输入10个整数
1:1
2:2
3:3
4:4
5:5
6:6
7:7
8:8
9:9
10:10
5
请输入10个浮点数
1:1.1
2:2.2
3:3.3
4:4.4
5:5.5
6:6.6
7:7.7
8:8.8
9:9.9
10:10.10
5.96
成功生成(总时间:28 秒)
最后,给出源码。请建2个.java
文件,分别放置这两个类。
第一个类:StuFee.java
package szu.ykl.calcStuFee;
/**
*
* @author 叶科良
*/
public class StuFee {
private double addSpeed;
private double baseFee;
private double fee;
public double getAddSpeed() {
return addSpeed;
}
public void setAddSpeed(double addSpeed) {
this.addSpeed = addSpeed;
}
public double calcFee(double year) {
return (Math.pow((1 + this.getAddSpeed()), year - 1)) * this.getBaseFee();
}
public double calcFee(int year1, int year2) {
if (year1 <= year2) {
double result = 0;
for (int i = 0; i < (year2 - year1 + 1); i++) {
result += this.calcFee(year1 + i);
}
return result;
}
return -1;
}
public StuFee(double addSpeed, double baseFee) {
this.setAddSpeed(addSpeed);
this.setBaseFee(baseFee);
}
public double getBaseFee() {
return baseFee;
}
public void setBaseFee(double baseFee) {
this.baseFee = baseFee;
}
public static int average(int[] array) {
if (null != array) {
if (array.length != 0) {
int resultInt = 0;
for (int i = 0; i < array.length; i++) {
resultInt += array[i];
}
return (int) (resultInt / array.length);
} else {
return -1;
}
} else {
return -1;
}
}
public static double average(double[] array) {
if (null != array) {
if (array.length != 0) {
double resultDouble = 0;
for (int i = 0; i < array.length; i++) {
resultDouble += array[i];
}
return (resultDouble / array.length);
} else {
return -1;
}
} else {
return -1;
}
}
}
第二个类:TestStuFee.java
package szu.ykl.calcStuFee;
import java.util.Scanner;
/**
*
* @author 叶科良
*/
public class TestStuFee {
public static void main(String[] args) {
StuFee test = new StuFee(0.05, 10000);
System.out.println("第十年学费:" + test.calcFee(10));
System.out.println("第11到第14年" + test.calcFee(11, 14));
TestStuFee testStu = new TestStuFee();
try {
System.out.println("请输入10个整数");
System.out.println(test.average(testStu.getAcceptAsIntArray(10)));
} catch (java.util.InputMismatchException e) {
System.out.println("请输入整数!");
}
try {
System.out.println("请输入10个浮点数");
System.out.println(test.average(testStu.getAcceptAsDoubleArray(10)));
} catch (java.util.InputMismatchException e) {
System.out.println("请输入浮点数!");
}
}
/**
* 指定数组大小,从命令行接收整数
* @param arraySize 数组大小
* @return 原始整数数组
*/
public int[] getAcceptAsIntArray(int arraySize) {
int[] acceptArray = new int[arraySize];
Scanner reader = new Scanner(System.in);
for (int i = 0; i < arraySize; i++) {
System.out.print((i + 1) + ":");
acceptArray[i] = reader.nextInt();
}
return acceptArray;
}
/**
* 指定数组大小,从命令行接收浮点数
* @param arraySize 数组大小
* @return 原始浮点数数组
*/
public double[] getAcceptAsDoubleArray(int arraySize) {
double[] acceptArray = new double[arraySize];
Scanner reader = new Scanner(System.in);
for (int i = 0; i < arraySize; i++) {
System.out.print((i + 1) + ":");
acceptArray[i] = reader.nextDouble();
}
return acceptArray;
}
}
最后,有问题可以追问!
谁帮我编写一个java程序:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
public class FindPrime {
public boolean isPrime(int m) {
for (int i = 2; i < m; i++) {
if (m == 2 || m == 3)
break;
else if (m % i == 0)
return false;
}
return true;
}
public int[] getPrime(int n) {
int num[] = new int[8000];
int j = 1;
for (int i = 2; i < n; i++) {
if (isPrime(i))
num[j++] = i;
}
num[0] = j;
return num;
}
public void save(int[] num) throws FileNotFoundException {
File file = new File("a.txt");
PrintStream ps = new PrintStream(file);
System.setOut(ps);
for (int i = 1; i < num[0]; i++) {
System.out.println(num[i]);
}
}
public void print(Integer n) {
String string = n.toString();
int[] num = new int[10];
String[] s = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
byte[] bytes = string.getBytes();
for (int i = 0; i < bytes.length; i++) {
num[bytes[i] - 48]++;
}
System.out.print(n);
for (int i = 0; i < num.length; i++) {
System.out.print(" " + s[i] + ":" + num[i]);
}
System.out.println();
}
public void printAll(int[] num) {
for (int i = 1; i < num[0]; i++) {
print(num[i]);
}
}
public static void main(String[] args) throws FileNotFoundException {
FindPrime fp = new FindPrime();
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
int[] a = fp.getPrime(i);
fp.printAll(a);
fp.save(a);
}
}
java8主程序是哪个
main
。在Java8程序中找到IDEA项目中的主程序,可以使用全文搜索,快捷方式是 Ctrl + Shift + F
,在输入框中输入 main
,就可以看到主程序入口了。
如何用JAVA程序在控制台输出"我是一个JAVA程序"?
通过 System.out.println
的方式输出到控制台。
定义 A.java
文件,并输出字符串:我是一个Java程序。
public class A {
public static void main(String[] args) {
System.out.println("我是一个JAVA程序");
}
}
java该怎么自学?
在学Java一定要问问自己喜欢吗?只要喜欢一切没问题,这是我的建议:多敲代码,多敲代码,多敲代码,重要的事情说三遍。 学习Java决不能听完敲一遍就过,一定要多敲几遍,第一遍可以跟着老师的思路,后面一定要有自己的思路,慢慢培养自己的编程思想和实现功能的逻辑。甚至可以自己给自己出题,完全靠自己实现一些有意思的小功能,对提升自己的能力和代码熟练度都有不小的帮助。
- 多看视频,多练习,主要是练习一些算法的运用。
- 建议下载一些编程练习题,一道一道弄通弄懂。
- 经常做笔记,用记事本把一些经典问题保存下来,以备复习。 如果是零基础的话,就看视频,B站上高旗老师讲得不错,每看到视频出现一段代码就马上在电脑上运行出来,不要直接拿源码光盘来复制,也不要视频看完半段了才开始敲代码。这样几天后你会找到一些感觉,然后没事就想些代码来写写,比如想下怎样把名字转换成乱码,然后再用另一个方法转换回来,模拟步话机发送几条消息什么的。这样下去不出3月你定牛逼,这时再把教材拿出来仔细看每一句话,掌握原理。 希望你早日学成,有苦恼一起讨论一下。