十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
刚写的,新鲜出炉
在白银区等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、成都做网站 网站设计制作按需网站设计,公司网站建设,企业网站建设,品牌网站设计,全网整合营销推广,外贸网站建设,白银区网站建设费用合理。
import java.util.ArrayList;
import java.util.Scanner;
public class LoopAndFunction {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int number;
ArrayListInteger theList = new ArrayListInteger();
System.out.print("Enter a number for list, end with -1: ");
number = input.nextInt();
while(number != -1){
theList.add(number);
System.out.print("Enter a number for list, end with -1: ");
number = input.nextInt();
}
System.out.println("the list is: " + theList.toString());
sortArray(theList);
System.out.println("after sorting, the list is: " + theList.toString());
for(int i = 0; i theList.size(); i++){
int eachNumber = theList.get(i);
if(isOddNumber(eachNumber)){
System.out.println(eachNumber + " is odd number");
}
else{
System.out.println(eachNumber + " is even number");
}
}
}
public static boolean isOddNumber(int number){
boolean isOdd = false;
if(number % 2 == 1){
isOdd = true;
}
return isOdd;
}
public static void sortArray(ArrayListInteger list){
for(int i = 0; i list.size() - 1; i++){
int temp = list.get(i);
for(int j = i + 1; j list.size(); j++){
if(list.get(i) list.get(j)){
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
}
}
讲解嘛,main方法就是调用两个方法的,通过Scanner, while循环让用户输入一些整数,以-1结束,然后先是排列数组,再使用for来挨个判断这些数字是否为偶数。isOddNumber很简单,就是判断一个整数是否为奇数,用%求余来实现。sortArray也很简单,就是排序一个整数型的list的。
public class ComplexFunction {
/**
* calculate result of f(x)
* @param x
* @return
*/
public double calFunctionFx(double x){
return this.calFunctionGx(Math.floor(Math.abs(x)), x);
}
/**
* Calculate value of function Gi(x), and return a result in double format
* @param i
* @param x
* @return the result of function Gi(x)
*/
private double calFunctionGx(double i,double x){
double gxResult = 0;
/*criterion 1: i = 0*/
if(i == 0){
gxResult = this.calFunctionHx(x);
}
/*criterion 2: i0*/
else{
gxResult = this.calFunctionHx(this.calFunctionGx(i - 1, x));
}
return gxResult;
}
/**
* Calculate value of function h(x), and return a result in double format.
* @param x
* @return the result of function H(x)
*/
private double calFunctionHx(double x){
double hxResult = 0;
/*criterion 1: x0 */
if(x 0){
hxResult = Math.pow(x - 1, 2);
}
/*criterion 2: 0= x =1*/
else if( 0 = x x = 1 ){
hxResult = x + 1;
}
/*criterion 3: x 1*/
else{
hxResult = -Math.sqrt(x - 1);
}
return hxResult;
}
/**
* For testing
* @param args
*/
public static void main(String[] args){
ComplexFunction complexFun = new ComplexFunction();
double result = complexFun.calFunctionFx(3.5);
System.out.println("The result of f(3.5) is " + result);
}
}
上面就是代码片段,ComplexFunction只有一个公开的方法calFunctionFx(),用于计算F(X)的值。另外有两个私有的方法calFunctionGx,calFunctionHx 分别用于计算G(X)和H(X),代码结构跟你提供的的函数结构是一样的,很容易看得明白的。
随便给你写了一个
package com.wys.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Test {
public static ListInteger smallNumbers,largeNumbers;
public static int sum1 = 0,sum2 = 0;
public static void main(String[] args) {
smallNumbers = new ArrayListInteger();
largeNumbers = new ArrayListInteger();
run();
}
public static void run() {
int i = 0;
for (int j = 0; j 50; j++) {
Random rand = new Random();
i = rand.nextInt(1000);
if (i500) {
small(i);
}else{
large(i);
}
}
System.out.println("随机输出的50个数字中:");
System.out.println("大于500的数(包括500)共有"+largeNumbers.size()+"个");
System.out.print("他们是"+largeNumbers);
System.out.println();
System.out.println("他们的和是"+sum1);
System.out.println("小于500的数共有"+smallNumbers.size()+"个");
System.out.print("他们是"+smallNumbers);
System.out.println();
System.out.println("他们的和是"+sum2);
}
private static void large(int number) {
largeNumbers.add(number);
sum1 += number;
}
private static void small(int number) {
smallNumbers.add(number);
sum2 += number;
}
}
帮你写好了.
public class AAA {
public static void main (String[] args){
double PI, y;
PI = 4 * Math.atan(1.0);
y = F(0.5*PI);
System.out.println(y);
}
public static double F (double x){
return Math.sin(x);
}
}