本文目錄一覽:
寫幾個簡單的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文件,分別放置這兩個類。
*******************第一個***************************
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;
}
};
}
********************第二個***************************************
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
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;im;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;in;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;inum[0];i++)
{
System.out.println(num[i]);
}
}
public void print(Integer n){
String string=n.toString();
int[] num=new int[10];
String[] s=new String[]{“零”,”一”,”二”,”三”,”四”,”五”,”六”,”七”,”八”,”九”};
byte[] bytes=string.getBytes();
for(int i=0;ibytes.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項目中的主程序,可以使用全文搜索,全文搜索在idea中的快捷方式是」ctrl+shift+f「組合鍵,使用組合鍵後,在輸入框中輸入「main」,就可以看到主程序入口了。
如何用JAVA程序在控制台輸出”我是一個JAVA程序”?
通過System.out.println的方式輸出到控制台
定義A.java文件,並輸出字元串 我是一個java程序
public class A{
public static void main(String[] args) {//定義main方法
System.out.println(“我是一個JAVA程序”);//輸出我是一個JAVA程序到控制台
}
}
java該怎麼自學?
在學java一定要問問自己喜歡嗎?只要喜歡一切沒問題,這是我的建議:多敲代碼,多敲代碼,多敲代碼,重要的事情說三遍
學習Java決不能聽完敲一遍就過,一定要多敲幾遍,第一遍可以跟著老師的思路,後面一定要有自己的思路,慢慢培養自己的編程思想和實現功能的邏輯。甚至可以自己給自己出題,完全靠自己實現一些有意思的小功能,對提升自己的能力和代碼熟練度都有不小的幫助。
多看視頻,多練習,主要是練習一些演算法的運用,建議下載一些編程練習題,一道一道弄通弄懂。經常做筆記,用記事本把一些經典問題保存下來,以備複習。
如果是零基礎的話,就看視頻,b站上高旗老師講得不錯,每看到視頻出現一段代碼就馬上在電腦上給他跑出來,不要直接拿源碼光碟來複制,不要把視頻看完半段了才開始敲代碼。這樣幾天後你會找到一些感覺,然後沒事就想些代碼來寫寫,比如想下怎樣把名字轉換成亂碼,然後再用另一個方法轉換回來,模擬步話機發送幾條消息什麼的。這樣下去不出3月你定牛逼,這時再把教材拿出來仔細看每一句話,掌握原理。
希望你早日學成,有苦惱一起討論一下。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/159830.html