按照你的要求编写的分段函数的Java程序如下
import java.util.Scannerpublic class Segment {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in)
double x=sc.nextDouble()
double y
if(x<-5){
y=3*x*x+2*x-1
}else if(x>=-5 && x<=5){
y=x*Math.sin(x)+Math.pow(2,x)
}else{
y=Math.sqrt(x-5)+Math.log10(x)
}
System.out.println("y="+y)
}
}
运行结果
输入 6
输出 y=1.7781512503836436
运行结果:
代码如下:
import java.util.Scannerpublic class App65 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in)
System.out.println("请输入x值:")
int x = scanner.nextInt()
int y = 0
if (x < 0) {
y = x * x
} else if (x >= 0 && x < 10) {
y = 2 * x - 1
} else if (x >= 10) {
y = 3 * x - 11
}
System.out.println("y=" + y)
}
}
大哥您的这个的题目明显是叫您用您的最好的美感去做一个界面,至于函数的实现您直接IF语句就OK了 ,您这个题目的目的在于界面,下次把要做的界面发过来。
我跟你来一个特脑残的看见就想砍人的界面。下面代码!
代码:
package com.lxp.p2015929
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JTextArea
public class SectionFunc extends JFrame {
private static final long serialVersionUID = System.currentTimeMillis()
private JTextArea textarea1 = null
private JTextArea textarea = null
public SectionFunc() {
this.layoutFrame()
}
private void layoutFrame() {
this.setTitle("分段函数计算")
this.setSize(400, 250)
this.setLayout(null)
JLabel messagelabel1 = new JLabel("X值")
messagelabel1.setSize(120, 30)
messagelabel1.setLocation(20, 20)
this.add(messagelabel1)
JLabel messagelabel2 = new JLabel("Y值")
messagelabel2.setSize(120, 30)
messagelabel2.setLocation(250, 20)
this.add(messagelabel2)
textarea = new JTextArea()
textarea.setSize(100, 120)
textarea.setLocation(20, 60)
this.add(textarea)
textarea1 = new JTextArea()
textarea1.setSize(100, 120)
textarea1.setLocation(250, 60)
this.add(textarea1)
JButton button = new JButton("计算")
button.setSize(100, 30)
button.setLocation(130, 120)
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SectionFunc.this.textarea1.setText(SectionFunc
.XToY(SectionFunc.this.textarea.getText()))
}
})
this.add(button)
this.setLocationRelativeTo(null)
this.setDefaultCloseOperation(EXIT_ON_CLOSE)
}
private static String XToY(String x) {
long tenx = Long.parseLong(x)
long teny = 0
if (tenx >0) {
teny = 3 * tenx - 1
} else if (tenx == 0) {
teny = -1
} else {
teny = 2 * tenx - 1
}
return String.valueOf(teny)
}
public static void main(String[] args) {
SectionFunc sectionfunc = new SectionFunc()
sectionfunc.setVisible(true)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)