小编Dar*_*loc的帖子

Java 中的自定义异常尝试抛出捕获

当变量 d1 和 d2 不是正确的数据类型时,我总是收到默认的 NumberFormatException 消息。

当使用 throw 语句条件捕获这些异常时,我想打印我的自定义异常消息。

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.print("Enter a numeric value: ");
    String input1 = sc.nextLine();
    Double d1 = Double.parseDouble(input1);
    System.out.print("Enter a numeric value: ");
    String input2 = sc.nextLine();
    Double d2 = Double.parseDouble(input2);
    System.out.print("Choose an operation (+ - * /): ");
    String input3 = sc.nextLine();
    try {
        if (!(d1 instanceof Double)) {
            throw (new Exception("Number formatting exception caused by: "+d1));
        }
        if (!(d2 instanceof Double)) { …
Run Code Online (Sandbox Code Playgroud)

java exception

0
推荐指数
1
解决办法
571
查看次数

更新的矢量元素不打印

在替换小于第一个和最后一个元素的平均值的元素后,下面的代码不会打印更新的向量元素。

如何改进此代码,用户输入指定长度的数组,并用平均值替换小于第一个和最后一个元素的平均值的每个元素。

是否有更好的方法将向量传递给函数,或更有效的方法来管理内存?

#include <iostream>
#include <vector>

std::vector<double> getData()
{
    int n;
    std::vector<double> intputData;
    std::cout << "Enter array length\n";
    std::cin >> n;
    std::cout << "Enter the numbers\n";
    for(int i = 0; i < n; i++){
        int tmpIn;
        std::cin >> tmpIn;
        intputData.push_back(tmpIn);
    }
    return intputData;
}


void modifyData(std::vector<double> &data_mo)
{
    double f=data_mo[0];
    double l=data_mo[data_mo.size() - 1];
    double av = (f + l)/2;
    for (auto j: data_mo)
    {
        if(j < av){
            j = av;
        }
    }
}

void printData(std::vector<double> &data_pr)
{
    for …
Run Code Online (Sandbox Code Playgroud)

c++ stl vector

0
推荐指数
1
解决办法
60
查看次数

将字符串分割成等长的部分c++

我想迭代地将任意长度的字符串分解为长度 = 80的子字符串,直到最终子字符串的大小小于 80 或 0。假设该字符串不能被 80 整除,因此最终子字符串的长度不一定是 80。我在这里做错了什么?

#include <string>
#include <math.h>

string sub;

for (int i = 0; i < ceil(str.length()/80); i++) {
            if(str.length()/80 >= 1){
                sub = str.substr(i*80, 80);
            }
            if(str.length()/80 == 0){
                 sub = str.substr(i*80, str.length()%80);
            }
            if(sub.length() <= 0){
                 return;
Run Code Online (Sandbox Code Playgroud)

c++ string loops substring

0
推荐指数
1
解决办法
1959
查看次数

标签 统计

c++ ×2

exception ×1

java ×1

loops ×1

stl ×1

string ×1

substring ×1

vector ×1