RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-475315

Charismatic's questions

Martin Hope
Charismatic
Asked: 2022-07-02 03:22:42 +0000 UTC

从字符串中读取单词

  • 0
package homeWork5;

import java.io.*;
import java.util.*;

public class WarAndWorldService1 {
    public void runSet() {
        try {
            FileReader fileReader = new FileReader("Module/Война и мир_книга.txt");
            Set<String> wordsWeNeed = new HashSet<>();
            int symbol;
            StringBuilder builder = new StringBuilder();
            while ((symbol = fileReader.read()) != -1) {
                if (symbol != ' ' && symbol != '\n' && symbol != ',' && symbol != '.' && symbol != '!' && symbol != ')' && symbol != '"'
                        && symbol != ':' && symbol != ';' && symbol != '?' && symbol != '*' && symbol != '(') {
                    builder.append((char) symbol);
                } else {
                    wordsWeNeed.add(builder.toString());
                    builder.setLength(0);
                }
            }
                wordsWeNeed.add(builder.toString());
                System.out.println(wordsWeNeed);
                System.out.println(wordsWeNeed.size());
            } catch(FileNotFoundException f){
                System.out.println("Ошибка. Файл не найден.");
            } catch(IOException e){
                System.out.println("Ошибка чтения файла.");
            }
        }
    }

问题是如何首先将文本文件数据转换为字符串。例如:

while((symbol = fileReader.read()) != -1) {
builder.append((char) symbol);
}
String result = builder.toString;

然后从这一行中提取单词(没有标点符号等)。我知道有这样的事情:

 String[] resultArr = result.split(" ");

但据我了解,在这种情况下,需要使用正则表达式。问题是,我可以从没有正则表达式的字符串中提取“纯”词吗?

java
  • 1 个回答
  • 10 Views
Martin Hope
Charismatic
Asked: 2022-06-20 17:02:14 +0000 UTC

Java泛型的比较

  • -1
public class Student<NU,N,A,R,O> {
    private final NU nu;
    private final N n;
    private final A a;
    private final R r;
    private final O o;

    public Student(NU nu, N n, A a, R r, O o) {
        this.nu = nu;
        this.n = n;
        this.a = a;
        this.r = r;
        this.o = o;
    }

    public N getN() {
        return n;
    }
    public A getA() {
        return a;
    }
    public R getR() {
        return r;
    }

    @Override
    public String toString() {
        return "\n Student[" +
                "Порядковый номер = " + nu +
                ", Имя = '" + n + '\'' +
                ", Возраст = " + a +
                ", Оценка = " + r +
                ", Олимпядник = " + o +
                ']' ;
    }
}

COMPARATOR 
public class StudentsComparatorsAgeAndRating implements Comparator<Student<Integer,String,Integer,Double,Boolean>> {

    @Override
    public int compare(Student o1, Student o2) {
        if (!(o1.getA().equals(o2))) {
            return (Integer)o1.getA() - (Integer)o2.getA();
        }
        return 0;
    }
}
public class StudentsComparatorsName implements Comparator<Student<Integer, String, Integer, Double, Boolean>> {

    @Override
    public int compare(Student o1, Student o2) {
        return (Integer)o1.getN().toString().length() - (Integer) o2.getN().toString().length();
    }
}
public class StudentsComparatorsRating implements Comparator<Student<Integer, String, Integer, Double, Boolean>> {
    @Override
    public int compare(Student o1, Student o2) {
        BigDecimal bd = BigDecimal.valueOf((Double)o1.getR());
        BigDecimal bd1 = BigDecimal.valueOf((Double)o2.getR());
        return bd1.compareTo(bd);
    }
}

问题是,我可以在没有类型转换的情况下比较泛型吗?

java
  • 1 个回答
  • 10 Views
Martin Hope
Charismatic
Asked: 2022-06-15 02:46:47 +0000 UTC

按索引从数组中删除元素

  • 0
public class DataContainer<T> {
    T[] data;
    DataContainer(T[] data) {
        this.data = data;
    }
    public int add(T item) {
        int i = 0;
        for (; i < data.length ; i++) {
                if (item == null) {
                    return -1;
                }
                if (data[i] == null) {
                    data[i] = item;
                    break;
                }
        }
        if (data.length >1&&data[i-1] != item) {
                data = Arrays.copyOf(data,data.length+1);
                data[i] = item;
        }
        if (data.length ==1&&data[i-1] != item ) {
                data = Arrays.copyOf(data,data.length+1);
                data[i] = item;
        }
        if (data.length==0) {
            data = Arrays.copyOf(data,1);
            if (data[i] != item ) {
                data[i] = item;
            }
        }
        return i;
    }

    public T[] getItem() {
        return data;
    }
    public T get(int index) {
        if (data.length <=index) {
            return null;
        }
       return data[index] ;
    }
    public boolean delete(int index) {
        for (int i = index; i < data.length; i++) {
            if (data[i] == data[index]) {
                for (int j = index; j < data.length; j++) {
                    data[i] = data[j];
                }
            }
            data = Arrays.copyOf(data,data.length -1);
            return true;
        }
        return false;
    }
}

MAIN 

public class DataContainerMain {
    public static void main(String[] args) {
        DataContainer<Integer> dc = new DataContainer<>(new Integer[]{1,2,3});
        System.out.println(dc.add(5));
        System.out.println(dc.delete(1));
        System.out.println(Arrays.toString(dc.getItem()));

    }
}

例如,在删除包含数据的数组时 - 1,2,3 我添加 5,然后我删除索引 1 处的元素,然后它应该变成 - 1,3,5,但结果是 1, 5,3 并且总是交换最后两个元素。为什么?

java
  • 3 个回答
  • 10 Views
Martin Hope
Charismatic
Asked: 2022-06-12 03:07:22 +0000 UTC

java.util.ConcurrentModificationException

  • 0
public class StudentMain {

    public static void main(String[] args) throws IOException {

        StudentService ss = new StudentService();
        List<Student<Integer,String,Integer,Double,Boolean>> students = new ArrayList<>();
        for (int i = 0; i < 10_000; i++) {
            students.add(new Student<>(ss.getNumber(),ss.getNameFromFileOutSide(), ss.getAge(), ss.getRating(),ss.getOlympic()));
        }
        List<Student<Integer, String, Integer, Double, Boolean>> ageEqualOrHigherTwelve;
        ageEqualOrHigherTwelve = students;

        Iterator<Student<Integer, String, Integer, Double, Boolean>> iterator = ageEqualOrHigherTwelve.iterator();
        for (Iterator<Student<Integer, String, Integer, Double, Boolean>> it = iterator; it.hasNext(); ) {
            Student<Integer, String, Integer, Double, Boolean> student = it.next();
            if (student.getA()>=12&&student.getR()>=8) {
                ageEqualOrHigherTwelve.add(student);
            }
        }
        System.out.println(ageEqualOrHigherTwelve);

    }
    }

为什么会抛出异常?以及如何解决?

java
  • 2 个回答
  • 10 Views
Martin Hope
Charismatic
Asked: 2022-05-31 15:46:12 +0000 UTC

将接口对象传递给 JAVA 构造函数

  • 0
public class CalculatorWithAutoAggregationSetup1  implements ICalculator {
    ICalculator ic;
    CalculatorWithOperator cwo;
    public CalculatorWithAutoAggregationSetup1(ICalculator ic) {
        this.ic = ic;

    }
    private int counter;
    private long memory;

    public CalculatorWithAutoAggregationSetup1() {

    }


    public double divide(double a, double b) {
        counter++;
        return ic.divide(a,b);
    }

    public double multiplication(double a, double b) {
        counter++;
        return a * b;
    }

    public double plus(double a, double b) {
        counter++;
        return a + b;
    }

    public double minus(double a, double b) {
        counter++;
        return a - b;
    }

    public double square(double a, int b) {
        double square = 1;
        for (int i = 0; i < b; i++) {
            square *= a;
        }
        counter++;
        return square;
    }

    public double module(double a) {
        if (a < 0) {
            a = -(a);
        }
        counter++;
        return a;
    }

    public double root(double a) {
        double x;
        if (a<0) {
            a = -(a);
        }
        double half = a / 2;
        do {
            x = half;
            half = (x + (a / x)) / 2;
        } while ((x - half) != 0);
        counter++;
        return half;
    }
    public long getCounter() {
        return  counter;
    }

我无法理解如果我们传递一个 ICalculator 类型的对象,那么我们将不得不在 main 中创建它(据我了解),但是如果不实现其方法就无法创建接口对象。另外,您需要在没有自己数学的情况下委托方法,但据我了解,没有其他类(包括接口)我无法委托(因为它没有实现)。问题是我应该将什么传递给构造函数,或者更确切地说如何(必须传递 ICalculator),以便我可以委托方法并运行 main。

java
  • 1 个回答
  • 10 Views
Martin Hope
Charismatic
Asked: 2022-05-21 13:25:05 +0000 UTC

溢出前乘以一个数字时,需要显示溢出前后的数字。不能使用负数。积极的一切都很好

  • 0

例如,第一个数字是 1。将它相乘直到溢出。对于正数,算法是明确的,对于负值则不清楚。例如数字 - 1 乘以 (- 2) 直到溢出

public class Loop5 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Введите число");
        long l = Long.parseLong(scan.next());
        Loop5 l5 = new Loop5();
        l5.longMinusValue(l);

    }

    public void longMinusValue(long l) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Введите число на которое умножать");
        long l1 = Long.parseLong(scan.next());
        while (Long.MAX_VALUE > l) {
            l *= l1;
            System.out.println(l);
        }

    }
}
java
  • 1 个回答
  • 10 Views
Martin Hope
Charismatic
Asked: 2022-05-17 15:03:42 +0000 UTC

我不明白如何检查这段代码中的小数。尝试了 if (x%1 !=0) 和 (x%2 ==1 && x%2 !=0) 并通过 NumberFormat。我不明白

  • -2
 import java.util.Scanner;
public class Loop1 {
    public static void main(String[] args) throws Exception {
        Loop1 l1 = new Loop1();
        l1.scannerNumber();
    }
    public void scannerNumber() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Введите число: ");
        long x = 0;
        long result = 1;
        int x1 = 0;
        int x2 = 0;
        while (x1 == 0 || x1 <0) {
            try {
                if (x2 == 5) {
                    System.out.println("Слишком много попыток. Программа закрывается");
                    System.exit(0);
                }
                x = Long.parseLong(scan.nextLine());
                x1++;
                if (x<0) {
                    System.out.println("Вы ввели отрицательное число");
                    x1--;
                    x2++;
                }
            } catch (Exception e) {
                System.out.println("Повтори попытку: ");
                x2++;
            }
        }
        if (x == 0) {
            System.out.println("Result = " + result);
            return;
        }
            for (int i = 0; i < x; i++) {
                if (result < 0) {
                    System.out.println("У вас получилось переполнение " + result);
                    break;
                } else if (result > 0){
                    System.out.println("Result = " + result);
                    result *= (x - i);
                }
            }
    }
}
java
  • 3 个回答
  • 10 Views
Martin Hope
Charismatic
Asked: 2022-05-15 13:53:25 +0000 UTC

通过从数组中删除 [a,b] 范围内的所有元素来压缩数组,并用零填充释放的元素。JAVA

  • 1
public class DeleteArray {

    public static void main(String[] args) {
        Random rand = new Random();
        Scanner scan = new Scanner(System.in);
        System.out.println("Введите размер массива - ");
        int x = scan.nextInt();
        int []  arr = new int[x];
        int [] arr1 = null;
        System.out.println("Введите значение a - ");
        int a = scan.nextInt();
        System.out.println("Введите значение b - ");
        int b = scan.nextInt();
        for (int i = 0;i< arr.length;i++) {
            arr[i] = rand.nextInt(10)+5;
        }
        System.out.println("" + Arrays.toString(arr));
        for (int i = 0; i < arr.length; i++) {
            if (i>=a && i<=b) {
                arr[i] = arr[i+1];
                arr[i] = 0;
            }
        }
        System.out.println("" + Arrays.toString(arr));
    }
}
java
  • 3 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5