我正在尝试解决一个基本功能.但是我的第二个if语句和else会出错.如果你能在这里给我一个帮助,那就是代码.
(define (equation x)
(if(> x 2) (+(-(* x x) x) 4) )
(if (and (> x 1 ) (= x 1)) (and (< x 2) (= x 2)) (/ 1 x))
(else 0)
)
Run Code Online (Sandbox Code Playgroud) 我的两个密码验证方法有问题.方法hasDigitsAndLetters应该检查字符串的所有字符是否是数字和字母,第二种方法hasTwoDigits应该检查传递中是否至少有两位数,但问题是对于预期的结果是真的它们是ruturning假.如果有人可以帮忙.这是代码.
//check if the whole string consists of digits and letters
public static boolean hasDigitsAndLetters(String pass)
{
for(int i=0; i<pass.length(); i++)
{
if(!Character.isLetterOrDigit((i)))
{
return false;
}
}
return true;
}
// check whether the password has at least 2 digits
public static boolean hasTwoDigits(String pass)
{
int counter = 0;
for(int i=0; i<pass.length(); i++)
{
if(Character.isDigit(i))
{
counter ++;
}
}
System.out.println("Number of digits: " + counter);
if(counter >= 2)
{
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud) 我试图实现一个简单的树形图来计算整数的出现,但它给了我一个NullPointerException,我不知道如何解决它.
Exception in thread "main" java.lang.NullPointerException
at exercises.CountOccurances_20_07.main(CountOccurances_20_07.java:21)
Run Code Online (Sandbox Code Playgroud)
这是代码:
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class CountOccurances_20_07
{
public static void main(String[] args)
{
int[] list = {2, 3, 40, 3, 5, 4, 3, 3, 3, 2, 0};
Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
for(int i: list)
{
int key = list[i];
if(list.length > 1)
{
if(map.get(key) == 0)
{
map.put(key, 1);
}
else
{
int value = map.get(key).intValue(); // line 21
value ++;
map.put(key, value); …Run Code Online (Sandbox Code Playgroud) 当我为月利率和1年投资金额4.25输入1000时,为什么我得到的结果是4.384414858452464E11而不是预期的1043.34?
import java.util.Scanner;
public class FinancialApplicationFutureInvestment_13 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter investment amount: ");
int investmentAmount = input.nextInt();
System.out.println("Enter monthly interest rate: ");
double monthlyInterestRate = input.nextDouble();
System.out.println("Enter number of years");
int numOfYears = input.nextInt();
double futureInvestmentValue = investmentAmount *
(Math.pow(1 + monthlyInterestRate, numOfYears * 12));
System.out.println("Accumulated value is: " + futureInvestmentValue);
double test = Math.pow(1 + monthlyInterestRate, numOfYears * 12);
System.out.println(test);
}
}
Run Code Online (Sandbox Code Playgroud) 我试图在框架中显示一个简单的jtable,但它没有显示表的列,我跟着oracle教程中的示例,但有一些我做得不对.该程序正在编译和运行.这是代码:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class SimpleTableDemo extends JPanel
{
SimpleTableDemo()
{
String[] columnNames = {"#",
"Repayment Dates",
"Principle Amount",
"Interest",
"Comission fee",
"Total installement amount",
"Principle balance"};
// BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.HALF_EVEN);
//each line of the two dimentional array is a line in the table
Object[][] data = {
{new Integer(1), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现责任链模式,但似乎我缺少一些东西,因为在具体的类中,setnexthandler没有设置下一个但总是相同的.我想我的错误是在else statems next.setNextHandler(next)中的processMalfunction()方法的具体类中; 我认为它应该是next.setNextHandler(Severity.Medium)的第一个.所以这是代码,如果你可以看看.这是代码.
public interface MalfunctionHandler
{
public void processMalfunction(Malfunction malfunciton);
public void setNextHandler(MalfunctionHandler handler);
}
public enum Severity
{
TRIVIAL, LOW, MEDIUM, HIGH
}
public class Malfunction
{
/**
* severity is a type of Severity
*/
Severity severity;
/**
* @param description describes the severity of the problem
*/
String description;
Malfunction(Severity severity, String description)
{
if(description == null)
{
description = "No description available. Probably serious.";
}
if(description.isEmpty())
{
description = "No description available. Probably serious."; …Run Code Online (Sandbox Code Playgroud) 我已经推出了一个Arraylist,我希望它是一个泛型类型.
ArrayList<T> locker = new ArrayList<T>();
Run Code Online (Sandbox Code Playgroud)
然后我有一个方法添加,也需要是通用的.
public <T extends Gear> boolean add(Gear item)
{
locker.add(item);// this is giving me compile error => no suitable method found for add(Gear)
return true;
}
Run Code Online (Sandbox Code Playgroud)
我该如何修复它,我也是通用类型的新手.
我试图实现冒泡排序,但我不确定它是否正确.如果你可以看看它是否是一个泡沫排序,可以更好地完成,请不要害羞.这是代码:
package Exercises;
import java.util.*;
public class BubbleSort_6_18
{
public static void main(String[] args)
{
Random generator = new Random();
int[] list = new int[11];
for(int i=0; i<list.length; i++)
{
list[i] = generator.nextInt(10);
}
System.out.println("Original Random array: ");
printArray(list);
bubbleSort(list);
System.out.println("\nAfter bubble sort: ");
printArray(list);
}
public static void bubbleSort(int[] list)
{
for(int i=0; i<list.length; i++)
{
for(int j=i + 1; j<list.length; j++)
{
if(list[i] > list[j])
{
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
} …Run Code Online (Sandbox Code Playgroud) 大家好,我真的被卡住,我一直在接受java.lang.NullPointerException.我试图在每个可能的地方处理它,但我没有成功地做到这一点.这是家庭作业.如果你能看一下并给出一些关于java.lang.NullPointerException它的反馈,那就太好了.异常发生在Captain.handleProblem() 和MalfucntionHandler.proccessMalfunction()
public abstract class MalfunctionHandler
{
MalfunctionHandler next;
/**
* severity is a type of Severity
*/
Severity severity;
/**
* @param description describes the severity of the problem
*/
String description;
/**
* @param f file object that refers to the log-silver.txt
*/
File f = new File("log-silver.txt");
MalfunctionHandler(Severity severity)
{
this.severity = severity;
}
public String getDescription()
{
if(description == null)
{
description = "No description available. Probably serious.";
}
return …Run Code Online (Sandbox Code Playgroud) java ×8
arraylist ×1
bubble-sort ×1
exception ×1
generics ×1
if-statement ×1
jscrollpane ×1
jtable ×1
passwords ×1
pow ×1
scheme ×1
swing ×1
treemap ×1
unit-testing ×1