package f;
import javautil;
public class Main {
static public int print(String line){
String str=linesubstring(1,linelength()-1);
List<String> list=ArraysasList(strsplit(","));
Systemoutprintln(list);
Set<String> set=new HashSet<String>();
setaddAll(list);
Systemoutprintln(set);
return setsize();
}
static public void main(String[] str){
String line="{苹果,香蕉,梨,葡萄,香蕉,樱桃,梨}";
print(line);
}
}
结果:
[苹果, 香蕉, 梨, 葡萄, 香蕉, 樱桃, 梨]
[葡萄, 香蕉, 樱桃, 梨, 苹果]
注意: ,是中文逗号
哈哈 我昨天刚做了这个作业 你够幸运
import javaawt;
import javaawtevent;
import javaxswing;
import javamath;
public class Homework10_3 {
public static void main(String args[]){
MathWindow win=new MathWindow();
}
}
class MathWindow extends JFrame{
JTextField text1,text2,text3;
JPanel ps,pn;
MathWindow(){
text1=new JTextField(10);
text2=new JTextField(10);
text3=new JTextField(10);
JButton button1,button2,button3,button4;
button1=new JButton("加");
button2=new JButton("减");
button3=new JButton("乘");
button4=new JButton("除");
ps=new JPanel();
pn=new JPanel();
pnadd(text1);
pnadd(text2);
pnadd(text3);
psadd(button1);
psadd(button2);
psadd(button3);
psadd(button4);
add(pn,BorderLayoutCENTER);
add(ps,BorderLayoutSOUTH);
setBounds(100,100,370,150);
setVisible(true);
validate();
button1addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s1=text1getText();
String s2=text2getText();
try{ BigInteger n1=new BigInteger(s1);
BigInteger n2=new BigInteger(s2);
n2=n1add(n2);
text3setText(n2toString());
}
catch(NumberFormatException ee){
text1setText(null);
text2setText(null);
}
}
});
button2addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s1=text1getText();
String s2=text2getText();
try{ BigInteger n1=new BigInteger(s1);
BigInteger n2=new BigInteger(s2);
n2=n1subtract(n2);
text3setText(n2toString());
}
catch(NumberFormatException ee){
text3setText("请输入数字字符");
text1setText(null);
text2setText(null);
}
}
});
button3addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s1=text1getText();
String s2=text2getText();
try{ BigInteger n1=new BigInteger(s1);
BigInteger n2=new BigInteger(s2);
n2=n1multiply(n2);
text3setText(n2toString());
}
catch(NumberFormatException ee){
text3setText("请输入数字字符");
text1setText(null);
text2setText(null);
}
}
});
button4addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s1=text1getText();
String s2=text2getText();
try{ BigInteger n1=new BigInteger(s1);
BigInteger n2=new BigInteger(s2);
if(n2toString()=="0"){
text3setText("除数不能为0");
}
else
{
n2=n1divide(n2);
text3setText(n2toString());
}
}
catch(NumberFormatException ee){
text3setText("请输入数字字符");
text1setText(null);
text2setText(null);
}
}
});
setDefaultCloseOperation(JFrameDISPOSE_ON_CLOSE);
}
}
两个问题,第一,要求是前1000项的和,你求的是前10000项,看这里:i<=(210000-1);
第二,1/i,你没有在这里进行强制转换,所以1/i是按照整数除法来进行的,也就是说所有1/i的值都是0。要这么写:(double)1/i。
你 for (j = 1; j <= 101-i; j++)
这里有问题,这里应该是用来算阶乘的,也就是应该从12到 i 咯,i是你循环到当前的那个数
for (j = 1,a = 1; j <= i; j++)
另外,你a=1的位置也错了,你放在for循环里面(我把a=1放到for的定义里就防止重复设值了),每次都是a=1重新设一下,根本没意义了
第一个问题: import javautilScanner; public class Demo { public static void main(String agrs[]){ Scanner input =new Scanner(Systemin); Systemoutprintln("请输入你要输入数字的个数:"); int len=inputnextInt(); Systemoutprintln("请输入一些数字:"); int num[]=new int[len]; int sum=0; double avg=0; for(int i=0;i<numlength;i++){ num[i]=inputnextInt(); sum+=num[i]; } avg=sum/numlength; Systemoutprintln("这些数字的和:"+sum); Systemoutprintln("这些数字的平均值班:"+avg); } } 第二个问题: import javautilScanner; public class Demo8 { public static void main(String args[]) { Systemoutprintln("请输入数字:"); Scanner input = new Scanner(Systemin); int num = inputnextInt(); int result = showNum(num); Systemoutprintln(num + "的阶层是:" + result); } //计算阶层的方法 public static int showNum(int num) { int result = 1; for (int i = 0; i < num; i++) { result = num - i; } return result; } } 第三个问题,递归,暂时没想起来
import javaio;
public class Cal
{
public static void main(String[] args)
{
float height=0;
float r=0;
double area=0;
try
{
while(true)
{
BufferedReader br=new BufferedReader(new InputStreamReader(Systemin));
Systemoutprintln ("请输入圆的高:");
try
{
height=FloatparseFloat(brreadLine());
Systemoutprintln ("请输入圆的半径:");
r=FloatparseFloat(brreadLine());
area=(rrMathPI);
Systemoutprintln ("面积为:"+area);
break;
}
catch(Exception ex)
{
Systemoutprintln ("请输入数字!");
}
}
}
catch(Exception ex)
{
exprintStackTrace();
}
}
}
以上就是关于一个java小程序不知有没有更简便的算法全部的内容,包括:一个java小程序不知有没有更简便的算法、编写一个Java小应用程序,设计4个按钮,分别命名为“加”、“差”、“积”、“除”以及3个文本框。、求高手帮纠正java小程序【计算1-1/3+1/5+.....的前1000项的和】等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)