我正在尝试为将字符串作为输入重新编写的程序编写代码.当用户没有放任何数据时,程序打印"Error",否则打印第一个字符串参数.
将没有数据称为"空"是否正确?这是行不通的.我该怎么写呢?
public class Try {
public static void main(String[] args){
if (args[0]==null){
System.out.println("Error- please type a string");
}else {System.out.println(args[0]);}
}
}
Run Code Online (Sandbox Code Playgroud) 我发现很难理解并使用java中的二进制表示:
在用户Jon Skeet的帮助下,我明白应该以这种方式构建二进制表示.
这是一个代码示例:
public class chack {
public static void main(String[] args) {
int num2=2;
int num3=3;
int num4=4;
int num1=1;
int nirbinary = (num1 << 24) | (num2 << 16) | (num3 << 8) | num4;
System.out.println(nirbinary);
String nir= Integer.toBinaryString(nirbinary);
System.out.println(nir);
}
}
Run Code Online (Sandbox Code Playgroud)
几个问题:
16909060我打印时为什么会这样nirbinary- 它代表什么?如何从已经处于此二进制表示形式的int中获取num1(例如)?谢谢
以下过程如何工作:
(define integers
(cons-stream 1
(stream-map (lambda (x) (+ x 1))
integers))
Run Code Online (Sandbox Code Playgroud) 这次我想从头到尾打印一个数组.
这就是我写的:
public class Arrays {
public static void main (String[] args){
for (int i = args.length; i >=0; i--){
System.out.print(args[i]+" ");
}
}
Run Code Online (Sandbox Code Playgroud)
这是错误消息:线程"main"中的异常java.lang.ArrayIndexOutOfBoundsException:4在Assignment02Q04.main(Assignment02Q04.java:5).
仍然很难实现Eclipse错误通知.我很乐意提供帮助.
我有这个问题:
该方法接受整数数组作为其输入,并返回一个新数组,该数组是输入数组的排列.方法fix34重新排列输入数组,使得每3个后面紧跟4(例如,如果在位置i处有3,则在位置i + 1处将存在4).该方法保留3s的原始位置,但可以移动任何其他数字,移动最小数量的数字.关于输入的假设:
好的,所以这就是我写的:
public class Fix34 {
public static void main(String[] args){
int [] args1 ={3,1,2,3,5,4,4};
int[] args11=fix34(args1);
for (int i = 0; i<=args11.length-1;i++ ){
System.out.print(args11[i]+" ");}}
public static int pos (int[] arr){
int i= arr.length-1;
while (arr[i]!=4){
i=-1;
}
return i;
}
public static int[] fix34(int[] nums){
for(int i = 0; i<=nums.length-1; i++){
if (nums[i] == 3){
nums[pos(nums)]=nums[i+1];
nums[i+1]=4;
}
}
return nums;
}
}
Run Code Online (Sandbox Code Playgroud)
当我插入这样的{3,2,1,4}数组时,它可以工作,但是在代码中编写的数组中,它给出了错误消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at …Run Code Online (Sandbox Code Playgroud) 作为学习编写Java的一部分,我在Web上找到了多个条件的"case"函数.
这个函数的问题在于它将参数与我用作条件的特定数字进行比较,但是如果我想每次为不同的数字范围比较参数,会发生什么呢?是否有比使用更优雅的方式很多"如果"的?更像是方案中的"cond"语法?
public class Assignment02Q03 {
public static void main(String[] args){
int grade=Integer.parseInt(args[0]);
if (grade >= 90) {
System.out.println("A");
}else {
if (grade>=80 ){
System.out.println("B");
}else {
if (grade>=70){
System.out.println("C");
}else {
if (grade>=60){
System.out.println("D");
}else {
System.out.println("F");
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
一定有更优雅的东西:)
谢谢!
我写了一个检查各种条件的代码.
如果它满足条件它会做它应该做的,否则我希望它抛出异常.
那有什么特殊的语法吗?否则,由于前置条件,编译器希望我返回任何我不想要的数组.
这是我的代码的一部分:
public static int [] code(int[]arr){
if ((arr!=null)&&(chack4and5(arr))&&(arr[arr.length-1]!=4)&&(TwoFours(arr))){
int k=0;
for(int i = 0; i<=arr.length-1; i++){
if (arr[i] == 4){
int place= pos(arr,k);
arr[place]=arr[i+1];
arr[i+1]=5;
k=k+3;
}
}
return arr;
}
else {
System.out.println("Please enter a legal array which matches the pre- conditions");
}
}
Run Code Online (Sandbox Code Playgroud)
}
这是关于接口和类的困扰我的东西.
我正在尝试通过名为IPAddressString的类对名为IPAddress的接口进行实现.Ipadress包含四个部分.
我正在编写一个名为mask的方法,该方法使用给定的掩码屏蔽当前地址.屏蔽操作是对地址的所有四个部分的按位'和'操作.你通过我写的名为getOctet的方法获得了所有这四个部分.(你可以在下面看到).
好的,所以我需要屏蔽我的this.IpAdress用它我用新的通用IPAddress编写了我的类.
在写掩码时,我遇到了问题.我计算了4个整数,我想要返回一个新的IPAddress类型.为了做到这一点,我需要使用我的constructer返回IPAddressString类型,而且我不能将IPAddressString转换为IPAddress.
我迷路了.我该怎么办?为什么我的结构不适合这个?IPAddressString不是IPAddress的子类型吗?
这是使代码更简单的代码:
这是界面:
public interface IPAddress {
/**
* Returns a string representation of the IP address, e.g. "192.168.0.1"
*/
public String toString();
/**
* Compares this IPAddress to the specified object
*
* @param other
* the IPAddress to compare this string against
* @return <code>true</code> if both IPAddress objects represent the same IP
* address, <code>false</code> otherwise.
*/
public boolean equals(IPAddress other);
/**
* Returns one of the four parts of the IP …Run Code Online (Sandbox Code Playgroud) 我希望您能帮助理解这种语法的含义:
class Node<K extends Comparable<? super K>, V>
Run Code Online (Sandbox Code Playgroud)
它?代表什么?
并没有人<失踪?
假设我想在"for"循环期间创建一个新字符串,
新字符串将是对exsist字符串的更改,该字符串将更改depand一个条件和循环的位置.
如何插入存在的字符串new chars?
我查看了与字符串相关的整个方法摘要,并没有得到我的答案.
编辑:原始我错误地发布了方法的java 4链接.我使用的是最新版本的java.
谢谢
我有这门课:
当我试图定义一个新的Instense时:
Point nir= new Point(double x, double y);
Run Code Online (Sandbox Code Playgroud)
我是,得到错误:
此行的多个标记 - x无法解析为变量 - y无法解析为变量
怎么会?我希望x和y是通用的,而不是特定的.我是在一个新的界面上写的. 这是班级
编辑:
我试图将点数类中的x0,y0的实现更改为一个点在一个名为"Circle"的给定界面中.
所以这是圈子的开头,我想做上面的事情:
public class Circle {
private double x0, y0, radius;
Run Code Online (Sandbox Code Playgroud)
因此,besicaly将x0,y0的表示更改为点结构.
我试图在你的帮助下知道我的代码是对还是错,因为遗憾的是我无法检查它.
没有编译错误.我想要做的是找到二叉树的高度.当然,树不必平衡.
public int height(RBNode t) {
if (t == null)
return 0;
int heightLeft = height(t.left);
int heightRight = height(t.right);
if (heightLeft > heightRight) {
return heightLeft + 1;
} else {
return (heightRight + 1);
}
}
Run Code Online (Sandbox Code Playgroud)
你认为递归条件是对的吗?我的朋友声称它总会返回0.