public void toString(String[] s) { for (String ss : s) { System.out.printf("%-20s ", ss); } }
时间: 2023-11-12 18:35:20 浏览: 42
这段代码是一个方法,用于将一个字符串数组s中的所有元素按照一定的格式输出。具体实现是使用for-each循环遍历字符串数组s中的每一个元素ss,并使用System.out.printf()方法将其按照格式输出。其中,%-20s表示左对齐并且宽度为20个字符,后面的逗号和ss表示要输出的字符串。最终,输出结果是每个字符串按照20个字符的宽度左对齐,中间用空格隔开。
相关问题
将以下代码改错:import java.math.*; class BigIntegerExample { public static void main(String args[]) { BigInteger n1=new BigInteger("987654321987654321987654321"), n2=new BigInteger("123456789123456789123456789"), result=null; result=【代码1】//n1和n2做加法运算 System.out.println("和:"+result.toString()); result=【代码2】//n1和n2做减法运算 System.out.println("差:"+result.toString()); result=【代码3】//n1和n2做乘法运算 System.out.println("积:"+result.toString()); result=【代码4】//n1和n2做除法运算 System.out.println("商:"+result.toString()); BigInteger m=new BigInteger("1968957"), COUNT=new BigInteger("0"), ONE=new BigInteger("1"), TWO=new BigInteger("2"); System.out.println(m.toString()+"的因子有:"); for(BigInteger i=TWO;i.compareTo(m)<0;i=i.add(ONE)) { if((n1.remainder(i).compareTo(BigInteger.ZERO))==0) { COUNT=COUNT.add(ONE); System.out.print(" "+i.toString()); } } System.out.println(""); System.out.println(m.toString()+"一共有"+COUNT.toString()+"个因子"); } }
import java.math.*;
class BigIntegerExample {
public static void main(String args[]) {
BigInteger n1 = new BigInteger("987654321987654321987654321"),
n2 = new BigInteger("123456789123456789123456789"),
result = null;
result = n1.add(n2); //n1和n2做加法运算
System.out.println("和:" + result.toString());
result = n1.subtract(n2); //n1和n2做减法运算
System.out.println("差:" + result.toString());
result = n1.multiply(n2); //n1和n2做乘法运算
System.out.println("积:" + result.toString());
result = n1.divide(n2); //n1和n2做除法运算
System.out.println("商:" + result.toString());
BigInteger m = new BigInteger("1968957"),
COUNT = new BigInteger("0"),
ONE = new BigInteger("1"),
TWO = new BigInteger("2");
System.out.println(m.toString() + "的因子有:");
for (BigInteger i = TWO; i.compareTo(m) < 0; i = i.add(ONE)) {
if ((m.remainder(i).compareTo(BigInteger.ZERO)) == 0) {
COUNT = COUNT.add(ONE);
System.out.print(" " + i.toString());
}
}
System.out.println("");
System.out.println(m.toString() + "一共有" + COUNT.toString() + "个因子");
}
}
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); Circle c1=new Circle(); System.out.println(c1); System.out.printf("%.2f\n",c1.getArea()); Circle c2=new Circle(); System.out.println(c2); System.out.printf("%.2f\n",c2.getArea()); c2.setRadius(sc.nextInt()); System.out.println(c2); System.out.printf("%.2f\n",c2.getArea()); Circle c3=new Circle(sc.nextInt()); System.out.println(c3); System.out.printf("%.2f\n",c3.getArea()); } } class Circle{ private int radius; public Circle() { this.radius=2; System.out.println("This is a constructor with no para."); } public Circle(int radius) { if(radius>0) { this.radius = radius; System.out.println("This is a constructor with para."); } else this.radius=2; } public void setRadius(int radius) { if(radius<=0) this.radius=2; else this.radius = radius; } public int getRadius() { return radius; } public double getArea(){ return radius*radius*Math.PI; } @Override public String toString() { return "Circle [radius=" + radius + "]"; } }
好的,这是一个Java程序,其中定义了一个Circle类,并在Main类中使用它进行测试。Circle类包含以下成员变量和方法:
成员变量:
- radius:表示圆的半径
方法:
- Circle():无参构造方法,将半径设置为2,输出"This is a constructor with no para."
- Circle(int radius):带参构造方法,如果传入的半径大于0,则将其作为半径;否则将半径设置为2。输出"This is a constructor with para."
- setRadius(int radius):设置半径,如果传入的半径小于等于0,则将半径设置为2。
- getRadius():获取半径。
- getArea():计算并返回圆的面积。
- toString():返回一个字符串,表示圆的半径。
在Main类中,使用Scanner类获取用户输入的半径,创建Circle对象,并输出圆的信息和面积。
希望这能解答您的问题。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)